【问题标题】:Functional test of email fails if tested action redirects如果测试的操作重定向,则电子邮件的功能测试失败
【发布时间】:2015-01-01 19:32:33
【问题描述】:

控制器操作会导致发送电子邮件,然后重定向。由于重定向,此操作的功能测试失败。如果控制器被重写以呈现模板,则测试通过。 [这似乎是普遍正确的;可以使用 [Symfony 的文档][1]] 中的代码复制这种情况。

编辑:测试失败

"/usr/bin/php" "/usr/bin/phpunit" "--colors" "--log-junit" “/tmp/nb-phpunit-log.xml” “--bootstrap” “/home/george/volunteer/app/bootstrap.php.cache” “--配置” “/home/george/volunteer/app/phpunit.xml.dist” “--filter” "%\btestActivateOrganization\b%" “/home/george/netbeans-8.0.1/php/phpunit/NetBeansSuite.php” “--run=/home/george/volunteer/src/Truckee/MatchingBundle/Tests/Controller/AdminControllerTest.php” Sebastian Bergmann 的 PHPUnit 3.7.28。

从 /home/george/volunteer/app/phpunit.xml.dist 读取配置

F

时间:1.36 秒,内存:40.75Mb

有 1 次失败:

1) Truckee\MatchingBundle\Tests\Controller\AdminControllerTest::testActivateOrganization 断言 0 与预期的 1 匹配失败。

/home/george/volunteer/src/Truckee/MatchingBundle/Tests/Controller/AdminControllerTest.php:64

失败!测试:1,断言:1,失败:1。

完成。

控制器

public function activateOrgAction($id)
{
    $em = $this->getDoctrine()->getManager();
    $organization = $em->getRepository("TruckeeMatchingBundle:Organization")->find($id);
    $temp = $organization->getTemp();
    if (true === $temp) {
        $organization->setTemp(false);
        $organization->setActive(true);
        $orgName = $organization->getOrgName();
        $em->persist($organization);
        $em->flush();
        $to = $em->getRepository("TruckeeMatchingBundle:Staff")->getActivePersons($id);
        $mailer = $this->container->get('admin.mailer');
        $mailer->activateOrgMail($organization, $to);
        $flash = $this->get('braincrafted_bootstrap.flash');
        $flash->success("$orgName has been activated");
    }

    return $this->redirect($this->generateUrl('admin_home'));
}

编辑 3:更完整的测试夹具

class AdminControllerTest extends WebTestCase
{

    private $client;

    public function setUp()
    {
        $classes = array(
            'Truckee\MatchingBundle\DataFixtures\SampleData\LoadFocusSkillData',
            'Truckee\MatchingBundle\DataFixtures\SampleData\LoadAdminUser',
            'Truckee\MatchingBundle\DataFixtures\SampleData\LoadStaffUserGlenshire',
            'Truckee\MatchingBundle\DataFixtures\SampleData\LoadStaffUserMelanzane',
            'Truckee\MatchingBundle\DataFixtures\SampleData\LoadTemplateData',
            'Truckee\MatchingBundle\DataFixtures\SampleData\LoadOpportunity',
            'Truckee\MatchingBundle\DataFixtures\SampleData\LoadVolunteer',
        );
        $this->loadFixtures($classes);
        $this->client = $this->createClient();
        $this->client->followRedirects();
    }

    public function login($user)
    {
        $crawler = $this->client->request('GET', '/login');
        $form = $crawler->selectButton('Login')->form();
        $form['_username'] = $user;
        $form['_password'] = '123Abcd';
        $crawler = $this->client->submit($form);

        return $crawler;
    }
    public function testActivateOrganization()
    {
        $crawler = $this->login('admin');
        $link = $crawler->selectLink('Accept organization')->link();
        $crawler = $this->client->click($link);

        $mailCollector = $this->client->getProfile()->getCollector('swiftmailer');
        $this->assertEquals(1, $mailCollector->getMessageCount());
    }
...
}

【问题讨论】:

  • 您在问题中写道“由于重定向,此操作的功能测试失败” - 您能否详细说明您的测试方式以及测试失败时的情况(例如,当您通过命令行 testrunner 运行测试时,请将输出显示到标准输出)。
  • @hakre:在上面的编辑中测试输出。测试在 Netbeans IDE 中作为重点测试方法运行。
  • 尝试从历史上的上一个请求中获取配置文件,因为客户端已经进行了重定向(我假设)。
  • @hakre:假设是正确的。测试setup() 包括$this->client->followRedirects();。
  • 创建客户端的代码在哪里?每个 Symfony 文档的单元测试中不应自动遵循重定向

标签: php symfony functional-testing swiftmailer


【解决方案1】:

将此添加到您的单元测试中:

 $this->client->followRedirects(false);

请参阅Symfony docs on Testing。不会自动遵循重定向,但您正在将它们设置为这样做。如果你想在测试完邮件后跟随下一个重定向,你可以调用j

$crawler = $client->followRedirect();

如果您想改回跟随所有重定向,请致电:

$client->followRedirects();

【讨论】:

  • 参见上面编辑的测试夹具。我假设在setup() 中执行$this->client->followRedirects(); 允许我不包括$this->client->followRedirect(); 在此夹具中可能发生重定向的任何地方。
  • setUp() 函数在客户端中为每个单元测试重新创建,因此如果您不希望它跟随特定测试,那么只需在该单独测试的顶部添加 $this->client->follow Redirects(false);
  • 令人着迷! $this->client->followRedirects(false); 的明智放置(即,不一定在顶部!)允许测试通过。非常感谢。
猜你喜欢
  • 1970-01-01
  • 2015-02-03
  • 1970-01-01
  • 1970-01-01
  • 2012-11-10
  • 2022-08-20
  • 2016-03-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多