用嘲弄测试Laravel外墙总是会通过,即使它应该失败

gla*_*ree 1 php unit-testing laravel mockery

我试图在单元测试中模拟Laravel的一些外观,但似乎测试总是通过无论如何.

例如,此示例取自Laravel文档:

Event::shouldReceive('fire')->once()->with('foo', array('name' => 'Dayle'));
Run Code Online (Sandbox Code Playgroud)

看起来我可以把它放在任何一种测试方法中,即使在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');
    }

}
Run Code Online (Sandbox Code Playgroud)

我为了测试外墙而缺少什么?我是否正确地认为我应该可以shouldReceive()在没有任何设置的情况下调用任何Laravel Facade?

Dav*_*all 6

你需要告诉嘲笑来运行它的验证.你可以通过推杆来做到这一点

\Mockery::close();
Run Code Online (Sandbox Code Playgroud)

在测试方法的最后,或在测试类的拆卸方法中.

或者,你可以通过将它添加到你的phpunit.xml来设置mockery的phpunit集成

<listeners>
  <listener class="\Mockery\Adapter\Phpunit\TestListener"></listener>
</listeners>
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请参阅http://docs.mockery.io/en/latest/reference/phpunit_integration.html.