在最终让我的愚蠢简单测试通过后,我感觉我没有正确地做到这一点.
我有一个SessionsController,负责显示登录页面并记录用户.
我决定不使用外观,这样我就不必延长Laravel的TestCase并在单元测试中获得性能.因此,我已通过控制器注入所有依赖项,如此 -
SessionsController - 构造函数
public function __construct(UserRepositoryInterface $user,
AuthManager $auth,
Redirector $redirect,
Environment $view )
{
$this->user = $user;
$this->auth = $auth;
$this->redirect = $redirect;
$this->view = $view;
}
Run Code Online (Sandbox Code Playgroud)
我已经完成了必要的变量声明和使用命名空间,我不打算将其包括在内,因为它是不必要的.
create方法检测用户是否被授权,如果是,则将其重定向到主页,否则显示登录表单.
SessionsController - 创建
public function create()
{
if ($this->auth->user()) return $this->redirect->to('/');
return $this->view->make('sessions.login');
}
Run Code Online (Sandbox Code Playgroud)
现在进行测试,我是新手,所以请耐心等待.
SessionsControllerTest
class SessionsControllerTest extends PHPUnit_Framework_TestCase {
public function tearDown()
{
Mockery::close();
}
public function test_logged_in_user_cannot_see_login_page()
{
# Arrange (Create mocked versions of dependencies)
$user = Mockery::mock('Glenn\Repositories\User\UserRepositoryInterface');
$authorizedUser = Mockery::mock('Illuminate\Auth\AuthManager');
$authorizedUser->shouldReceive('user')->once()->andReturn(true);
$redirect …Run Code Online (Sandbox Code Playgroud)