我试图在单元测试期间模拟 Laravel 中的一些外观,但似乎无论如何测试总是会通过。
例如,此示例取自 Laravel 文档:
Event::shouldReceive('fire')->once()->with('foo', array('name' => 'Dayle'));
看来我可以将其放入任何测试方法中,即使没有进行任何此类测试,它们也总是通过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' => '[email protected] /cdn-cgi/l/email-protection',
'inputPassword' => 'badpassword'
]);
// Should redirect back to login form with old input
$this->assertHasOldInput();
$this->assertRedirectedToAction('SessionsController@getLogin');
}
}
为了测试 Facades,我缺少什么?我认为我应该能够打电话对吗?shouldReceive()
在没有任何设置的任何 Laravel Facade 上?
您需要告诉嘲笑来运行它的验证。你可以通过放置来做到这一点
\Mockery::close();
无论是在测试方法的末尾,还是在测试类的拆卸方法中。
或者,您可以通过将其添加到 phpunit.xml 来设置嘲笑的 phpunit 集成
<listeners>
<listener class="\Mockery\Adapter\Phpunit\TestListener"></listener>
</listeners>
See http://docs.mockery.io/en/latest/reference/phpunit_integration.html http://docs.mockery.io/en/latest/reference/phpunit_integration.html了解更多信息。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)