我正在尝试在单元测试期间模拟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

关于php - 用 mock 测试Laravel外墙始终会通过,即使它应该失败,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24028829/

10-14 13:06
查看更多