繁体   English   中英

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

[英]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.

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