我想使用LiipFunctionalTestBundle在Symfony中测试该命令。因此,该命令将发送电子邮件。我想检查一下是否在邮件服务中执行了send,或者我的存储库中的方法是否正在运行。
测试失败,因为setAsNotified没有执行,但它确实执行了。
namespace Tests\AppBundle\Controller;
use AppBundle\Command\NewMessageEmailCommand;
use Liip\FunctionalTestBundle\Test\WebTestCase;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Console\Tester\CommandTester;
class NewMessageEmailCommandTest extends WebTestCase
{
public function setUp()
{
$this->fixtures = $this->loadFixtureFiles(array(
'@AppBundle/DataFixtures/ORM/users.yml',
'@AppBundle/DataFixtures/ORM/messages.yml',
));
}
public function testExecute()
{
$kernel = $this->createKernel();
$kernel->boot();
$msgRep = $this->getContainer()->get('doctrine.orm.default_entity_manager')->getRepository('AppBundle:Message');
$stub2 = $this->getMockBuilder(get_class($msgRep))
->disableOriginalConstructor()
->setMethods(['setAsNotified'])
->getMock();
$stub2->expects($this->atLeastOnce())
->method('setAsNotified')
->willReturn(true);
$this->runCommand('email:message_received:send',[
'--limit' => 1
], true);
}
}发布于 2017-01-06 18:17:36
测试命令的一种更简单的方法是将命令的逻辑移动到服务中。该服务将比SF命令更容易测试。
https://stackoverflow.com/questions/38137623
复制相似问题