我只是试着写一个Auth
一个简单的测试:认证试运行奇怪
use Mockery as m;
...
public function testHomeWhenUserIsNotAuthenticatedThenRedirectToWelcome() {
$auth = m::mock('Illuminate\Auth\AuthManager');
$auth->shouldReceive('guest')->once()->andReturn(true);
$this->call('GET', '/');
$this->assertRedirectedToRoute('general.welcome');
}
public function testHomeWhenUserIsAuthenticatedThenRedirectToDashboard() {
$auth = m::mock('Illuminate\Auth\AuthManager');
$auth->shouldReceive('guest')->once()->andReturn(false);
$this->call('GET', '/');
$this->assertRedirectedToRoute('dashboard.overview');
}
这是代码:
public function getHome() {
if(Auth::guest()) {
return Redirect::route('general.welcome');
}
return Redirect::route('dashboard.overview');
}
当我跑,我有以下错误:
EF.....
Time: 265 ms, Memory: 13.00Mb
There was 1 error:
1) PagesControllerTest::testHomeWhenUserIsNotAuthenticatedThenRedirectToWelcome
Mockery\Exception\InvalidCountException: Method guest() from Mockery_0_Illuminate_Auth_AuthManager should be called
exactly 1 times but called 0 times.
—
There was 1 failure:
1) PagesControllerTest::testHomeWhenUserIsAuthenticatedThenRedirectToDashboard
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'http://localhost/dashboard/overview'
+'http://localhost/welcome'
我的问题是:
两个类似的测试用例,但为什么错误输出不同?第一个模拟
Auth::guest()
不叫,而第二个似乎被称为。在第二个测试案例中,它为什么会失败?
有什么办法可以为我的代码编写更好的测试吗?或者甚至更好的代码来测试。
以上测试用例,我用
Mockery
来模拟AuthManager
,但如果我使用门面Auth::shoudReceive()->once()->andReturn()
,那么它最终会起作用。这里有Mockery
和Auth::mock
立面有什么不同吗?
谢谢。
我刚刚误认为是没有创建模拟对象'$这个 - > APP->实例( )'。 :)无论如何, – 2014-11-19 16:00:18
还要确保如果你有' - >中间件('auth')'设置路径,你将这个特征添加到你的测试中'Illuminate \ Foundation \ Testing \ WithoutMiddleware' – 2016-10-12 19:21:06