首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如果测试操作重定向,电子邮件的功能测试将失败。

如果测试操作重定向,电子邮件的功能测试将失败。
EN

Stack Overflow用户
提问于 2015-01-01 19:32:33
回答 1查看 233关注 0票数 1

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

编辑:测试失败

"/usr/bin/php“/usr/bin/phpunit”-colors“-log.xml "/tmp/nb-phpunit-log.xml”--引导“"/home/george/volunteer/app/bootstrap.php.cache”“--配置”/home/george/志愿/app/phpinit.xml.dist“-过滤器”%btestActivateOrganization\b%“"/home/george/netbeans-8”.0.1/php/ PHPUnit /NetBeansSuite.php“"--run=/home/george/volunteer/src/Truckee/MatchingBundle/Tests/Controller/AdminControllerTest.php”PHPUnit 3.7.28“,Sebastian Bergmann著。 配置读自/home/george/志愿/app/phminit.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。 好了。

控制器

代码语言:javascript
复制
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:更完整的测试夹具

代码语言:javascript
复制
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());
    }
...
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-01-01 19:51:35

将此添加到单元测试中:

代码语言:javascript
复制
 $this->client->followRedirects(false);

关于测试的Symfony文档。重定向不会自动执行,但是您将它们设置为这样做。如果您想在测试电子邮件后进行下一次重定向,您可以调用j。

代码语言:javascript
复制
$crawler = $client->followRedirect();

如果要更改为跟踪所有重定向,请调用:

代码语言:javascript
复制
$client->followRedirects();
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27733953

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档