[英]Testing Laravel facades with mockery always passes, even when it should fail
我正在嘗試在單元測試期間模擬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,我缺少什么? 我是否認為我應該能夠在沒有任何設置的情況下在任何Laravel Facade上調用shouldReceive()
嗎?
您需要告訴嘲笑來運行它的驗證。 你可以把
\Mockery::close();
在測試方法的末尾,或在測試類的拆卸方法中。
另外,您可以通過將其添加到phpunit.xml中來設置嘲笑的phpunit集成
<listeners>
<listener class="\Mockery\Adapter\Phpunit\TestListener"></listener>
</listeners>
有關更多信息,請參見http://docs.mockery.io/en/latest/reference/phpunit_integration.html 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.