从 5.0 升级到 5.1 - 非模拟测试不会通过
Upgrate from 5.0 to 5.1 - Now mocked tests won't pass
不知道有没有人能帮我解决这个问题。在 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());
}
}
过去一个小时我一直在用头撞屏幕,我想不通,这肯定是愚蠢的...
我设法通过在每个需要它的测试用例中模拟 Queue::push
和 Queue::connected
方法来解决这个问题,因为有些测试不关心队列。像这样:
\Queue::shouldReceive('push')->atLeast();
\Queue::shouldReceive('connected')->atLeast();
以防万一它能帮助人们摆脱困境,因为那真是令人头疼!
不知道有没有人能帮我解决这个问题。在 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());
}
}
过去一个小时我一直在用头撞屏幕,我想不通,这肯定是愚蠢的...
我设法通过在每个需要它的测试用例中模拟 Queue::push
和 Queue::connected
方法来解决这个问题,因为有些测试不关心队列。像这样:
\Queue::shouldReceive('push')->atLeast();
\Queue::shouldReceive('connected')->atLeast();
以防万一它能帮助人们摆脱困境,因为那真是令人头疼!