【问题标题】:Functional test for Email as Service in Symfony3Symfony3 中的电子邮件即服务功能测试
【发布时间】:2017-08-05 12:58:32
【问题描述】:

我正在尝试编写一个检查电子邮件是否发送的功能测试。我的 sendemail 方法是在服务中定义的,所以我无法遵循cookbook 中给出的示例

设置 - 控制器有一个表单,当表单提交并且有效时会调用服务中的sendmail方法

控制器

public function sampleAction(Request $request){

   $sampleEntity = new SampleEntity();
   $form = $this->createForm(sampleType::class, $sampleEntity)
   $form->handleRequest($request);
   if ($form->isSubmitted() && $form->isValid()){
      ...
      $sampleservice = $this->get('sampleservice');
      $sampleservice->sendMail();

      return $this->render('sample/formsubmitted.html.twig');
   }
}

服务

public function sendMail(){

        $body = $this->renderTemplate();

        $message = \Swift_Message::newInstance()
            ->setSubject('Sample Mail')
            ->setFrom($this->sender_mail)
            ->setTo($this->admin_mail)
            ->setBody($body);

        $this->mailer->send($message);
    }

测试

public function testEmailSend(){

        $client = static::createClient();

        $client->enableProfiler();
        $crawler = $client->request('POST', '/sampleform');
        $client->followRedirects(false);

        $form = $crawler->selectButton('Submit Form')->form();

        $crawler = $client->submit($form);

        $mailCollector = $client->getProfile()->getCollector('swiftmailer');

        $this->assertEquals(1, $mailCollector->getMessageCount());

    }

能否更好地解释这一行 $crawler = $client->request('POST', '/sampleform'); ,在食谱中它说明了 sendmail 操作的路径,但由于在我的情况下它不是操作,所以在那里写什么还是我必须采取全新的方法?

除此之外,我尝试直接从服务调用 sendmail,assertEqual 给我 0 因为 sendmail 未被访问(我猜)。关于在哪里以及如何进行的任何想法。这是我第一次尝试编写功能测试,所以如果我犯了一些明显的错误,请原谅我。

编辑 1

样本类型

public function buildForm(FormBuilderInterface $builder, array $options){

   $builder
            ->add('name', TextType::class, array('data' => 'Sample'))
            ->add('phone', TextType::class, array('data' => '1234567890'))
->add('save', SubmitType::class, array('label' => 'Submit Form'))
            ->getForm();    
}

编辑 2 直接调用service的时候,有没有其他方法可以直接调用service?

public function testEmailSend(){

        $client = static::createClient();
        $client->enableProfiler();
        $crawler = $client->request('POST', '/src/AppBundle/Service/SampleService/sendMail()');

        $mailCollector = $client->getProfile()->getCollector('swiftmailer');

        $this->assertEquals(1, $mailCollector->getMessageCount());

    }

这个返回“断言 0 匹配预期的 1 失败。”

邮件在应用程序中成功发送,但也要确保在测试中

【问题讨论】:

  • 您确定测试进入if ($form->isSubmitted()... 操作吗?因为你发帖没有价值。你的表单类型是什么样的?
  • 是的,当if () 为真时,我已经编写了测试。我已经编写了真假测试。因此,也尝试扩展 sendmail 的真正测试。让我们假设表单中的所有值都有预定义的数据。
  • 尝试仅调用您的电子邮件服务的获取操作。它在开发模式下工作吗?
  • 从我在说明书条目中看到的,enableProfiler 只为下一个请求启用分析器。您的提交是第二个请求。在测试中提交表单之前启用分析器。
  • 在您的config_test.yml 中也有profiler enabled 和collect?见本页底部:symfony.com/doc/current/testing/profiling.html

标签: php symfony functional-testing swiftmailer


【解决方案1】:

从我在说明书条目中看到的,enableProfiler 只为下一个请求启用分析器。您的提交是第二个请求。在您的测试中提交表单之前启用分析器。

您的config_test.yml 中是否还有分析器enabled: true 和collect: true?见本页底部:How to Use the Profiler in a Functional Test

测试

public function testEmailSend(){

        $client = static::createClient();

        $crawler = $client->request('POST', '/sampleform');
        $client->followRedirects(false);

        $form = $crawler->selectButton('Submit Form')->form();

        $client->enableProfiler();
        $crawler = $client->submit($form);

        $mailCollector = $client->getProfile()->getCollector('swiftmailer');

        $this->assertEquals(1, $mailCollector->getMessageCount());

    }

【讨论】:

    猜你喜欢
    • 2019-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多