用嘲讽测试Laravel外墙始终会通过,即使它应该失败 [英] Testing Laravel facades with mockery always passes, even when it should fail
问题描述
我正在尝试在单元测试期间在Laravel中模拟某些外观,但是无论如何,测试似乎总是可以通过.
I'm trying to mock some facades in Laravel during unit testing, but it seems that the tests always pass no matter what.
例如,此示例取自Laravel文档:
For example, this example taken from the Laravel docs here:
Event::shouldReceive('fire')->once()->with('foo', array('name' => 'Dayle'));
我似乎可以在任何测试方法中使用它,即使Event
外观没有进行任何排序,它们也始终可以通过.
It seems I can put that in any of the test methods and they always pass even though nothing of the sort has been done with the Event
facade.
这是测试班:
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,我缺少什么?我是否认为我应该可以在任何Laravel Facade上不进行任何设置即可调用shouldReceive()
?
What am I missing in order to test Facades? Am I right in thinking that I should be able to call shouldReceive()
on any Laravel Facade without any setup?
推荐答案
您需要告诉嘲笑来运行它的验证.您可以通过放置
You need to tell mockery to run it's verification. You can do that by putting
\Mockery::close();
要么在测试方法的末尾,要么在测试类的拆卸方法中.
Either at the end of your test method, or in your test class' teardown method.
或者,您可以通过将其添加到phpunit.xml中来设置嘲笑的phpunit集成
Alternatively, you could set up mockery's phpunit integration by adding this to your phpunit.xml
<listeners>
<listener class="\Mockery\Adapter\Phpunit\TestListener"></listener>
</listeners>
请参见 http://docs.mockery.io/en/latest/reference/phpunit_integration.html 获取更多信息.
这篇关于用嘲讽测试Laravel外墙始终会通过,即使它应该失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!