从5.0升级到5.1 - 现在模拟测试不会通过

时间:2015-08-13 09:50:33

标签: php laravel-5 phpunit laravel-5.1 mockery

我想知道是否有人可以帮我解决这个问题。在Laravel 5.0下,我的所有测试都运行良好,但是自从今天早上更新到5.1(.17)后,任何使用Mockery的测试现在都失败了,类似于:

vagrant@homestead:~/Code/nps$ php phpunit.phar tests/Workers/AutomatedEmailerWorkerTest.php
PHPUnit 4.6.2 by Sebastian Bergmann and contributors.

Configuration read from /home/vagrant/Code/nps/phpunit.xml.dist

E

Time: 16.27 seconds, Memory: 56.50Mb

There was 1 error:

1) TRP\Nps\Tests\Workers\AutomatedEmailerWorkerTest::testEmail
Mockery\Exception\InvalidCountException: Method connected() from Mockery_0_Illuminate_Queue_QueueManager should be called
 exactly 1 times but called 4 times.

有问题的测试:

class AutomatedEmailerWorkerTest extends TestCase
{
    private $autoWorker;
    private $returned;
    private $payload = [
        'content' => 'PHPUnit Test Email Content',
        'subject' => 'PHPUnit Test Email Subject',
        'blade_template' => 'emails.interaction',
        'closed_loop_step_id' => '1',
    ];


    public function testEmail()
    {
        Mail::pretend(false);
        self::pretendQueue();

        $mock = Mockery::mock(
            'Swift_Mailer[send]',
            [
                Mockery::mock('Swift_Transport')->shouldIgnoreMissing()
            ]
        );

        Mail::setSwiftMailer($mock);
        $globalMessage = null;

        $mock->shouldReceive('send')->atLeast()->times(1)
            ->andReturnUsing(function ($msg) use (&$globalMessage) {
                $globalMessage = $msg;
            });

        $response = $this->call(
            'POST',
            '/api/v1/automatedemail',
            $this->payload,
            [],
            [],
            ['HTTP_AUTHORIZATION' => 'Bearer '.$this->getAuthToken()],
            []
        );

        $this->assertEquals($this->payload['subject'], $globalMessage->getSubject());
        $this->assertContains($this->payload['content'], $globalMessage->getBody());
        $this->assertNotEmpty($globalMessage->getTo());
        $this->assertNotEmpty($globalMessage->getFrom());
    }
}

过去一小时我一直在屏幕上撞我的脑袋,我无法弄明白,这肯定是愚蠢的......

1 个答案:

答案 0 :(得分:0)

由于一些测试不关心队列,我通过在每个需要它的测试用例中模拟Queue::pushQueue::connected方法来设法解决这个问题。像这样:

\Queue::shouldReceive('push')->atLeast();
\Queue::shouldReceive('connected')->atLeast();

以防万一它可以帮助别人,因为这是一个令人头疼的问题!