symfony 4表单单元测试不起作用

时间:2018-06-28 14:44:55

标签: phpunit symfony4

嗨,我正在尝试用symfony 4测试表单。这是我的代码,

$crawler = $this->client->request('GET', '/admin/user/new');

        $form = $crawler->selectButton('Save')->form(array(
            'user[displayName]' => 'user',
            'user[username]' => 'user@yahoo.com',
            'user[password]' => 'user123',
            'user[phoneNumber]' => '1234789',
            'user[roles]' => 'ROLE_USER',
        ));
        $crawler = $this->client->submit($form);
        $this->assertGreaterThan(
                0, $crawler->filter('html:contains("Your changes were saved!")')->count()
        );

表单具有值,但我得到的只是

  

无法断言0大于0。

当实际提交时,我会收到此警报。

<div class="alert alert-success alert-dismissible">
    Your changes were saved!
</div>

如果有人可以帮助

1 个答案:

答案 0 :(得分:0)

由于始终提交表单提交,因此似乎需要遵循重定向。

$crawler = $this->client->request('GET', '/admin/user/new');

        $form = $crawler->selectButton('Save')->form(array(
            'user[displayName]' => 'user',
            'user[username]' => 'user@yahoo.com',
            'user[password]' => 'user123',
            'user[phoneNumber]' => '1234789',
            'user[roles]' => 'ROLE_USER',
        ));
        $crawler = $this->client->submit($form);

        $crawler = $this->client->followRedirect();

        $this->assertGreaterThan(
                0, $crawler->filter('html:contains("Your changes were saved!")')->count()
        );