2

我试图在单元测试期间模拟 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,我缺少什么?我是否认为我应该能够shouldReceive()在没有任何设置的情况下调用任何 Laravel Facade?

4

1 回答 1

7

你需要告诉 mockery 运行它的验证。你可以通过把

\Mockery::close();

在您的测试方法结束时,或者在您的测试类的拆卸方法中。

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

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

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

于 2014-06-04T08:55:25.403 回答