我试图在单元测试期间模拟 Laravel 中的一些外观,但似乎无论如何测试总是通过。
例如,这个例子取自 Laravel 文档:
Event::shouldReceive('fire')->once()->with('foo', array('name' => 'Dayle'));
看来我可以把它放在任何测试方法中,即使Event
外观没有做任何事情,它们也总是通过。
这是测试类:
class SessionsControllerTest
extends TestCase
{
public function test_invalid_login_returns_to_login_page()
{
// All of these pass even when they should fail
Notification::shouldReceive('error')->once()->with('Incorrect email or password.');
Event::shouldReceive('fire')->once()->with('foo', array('name' => 'Dayle'));
Notification::shouldReceive('nonsense')->once()->with('nonsense');
// Make login attempt with bad credentials
$this->post(action('SessionsController@postLogin'), [
'inputEmail' => 'bademail@example.com',
'inputPassword' => 'badpassword'
]);
// Should redirect back to login form with old input
$this->assertHasOldInput();
$this->assertRedirectedToAction('SessionsController@getLogin');
}
}
为了测试 Facades,我缺少什么?我是否认为我应该能够shouldReceive()
在没有任何设置的情况下调用任何 Laravel Facade?