简体   繁体   English

用嘲讽测试Laravel外墙始终会通过,即使它应该失败

[英]Testing Laravel facades with mockery always passes, even when it should fail

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: 例如,此示例取自Laravel文档:

Event::shouldReceive('fire')->once()->with('foo', array('name' => 'Dayle'));

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. 似乎我可以在任何测试方法中使用它,即使Event外观未做任何排序,它们也始终可以通过。

Here is the test class: 这是测试类:

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');
    }

}

What am I missing in order to test Facades? 为了测试Facades,我缺少什么? Am I right in thinking that I should be able to call shouldReceive() on any Laravel Facade without any setup? 我是否认为我应该能够在没有任何设置的情况下在任何Laravel Facade上调用shouldReceive()吗?

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. 在测试方法的末尾,或在测试类的拆卸方法中。

Alternatively, you could set up mockery's phpunit integration by adding this to your phpunit.xml 另外,您可以通过将其添加到phpunit.xml中来设置嘲笑的phpunit集成

<listeners>
  <listener class="\Mockery\Adapter\Phpunit\TestListener"></listener>
</listeners>

See http://docs.mockery.io/en/latest/reference/phpunit_integration.html for further information. 有关更多信息,请参见http://docs.mockery.io/en/latest/reference/phpunit_integration.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM