我们说我有这个控制器用于身份验证:
class AuthController extends BaseController
{
public function __construct(User $user)
{
$this->user = $user;
}
public function getLogin()
{
if (Auth::check()) {
Session::flash('alert-type', 'error');
Session::flash('alert-message', 'You are already logged in');
return Redirect::to('home');
}
return View::make('auth/login');
}
}
我有这个单元测试:
public function testGetLoginWithoutLogin()
{
Auth::shouldReceive('check')->once()->andReturn(false);
View::shouldReceive('make')->once();
$userMock = Mockery::mock('Eloquent', 'User');
$authController = new AuthController($userMock);
$authController->getLogin();
}
我如何确保返回视图?在有效登录的不同单元测试中,我将如何测试它返回重定向?
答案 0 :(得分:0)
您可以检查getLogin()
的返回值的类型。
重定向
$returnValue = $authController->getLogin();
$this->assertInstanceOf('\Illuminate\Http\RedirectResponse', $returnValue);
对于视图
$returnValue = $authController->getLogin();
$this->assertInstanceOf('\Illuminate\View\View', $returnValue);
答案 1 :(得分:0)
使用with()
检查您的观点是否获得了正确的模板:
View::shouldReceive('make')->once()->with('auth/login');
...然后检查重定向,有一些useful assertions that come built in to Laravel,所以:
// once you make your simulated request
$this->call('GET', 'URL-that-should-redirect');
// (or simulate a get to the action on your Controller.. )
$this->action('GET', 'Controller@action');
//you can check that a redirect simply happens:
$this->assertResponseStatus(302); // would also work with any HTTP status
// ...or even check that you get redirected to
// a particular URL ...
$this->assertRedirectedTo('foo');
// ... route ...
$this->assertRedirectedToRoute('route.name');
// ...or controller action
$this->assertRedirectedToAction('Controller@method');