如何测试用户登录页面?

时间:2016-02-28 18:45:40

标签: symfony phpunit fosuserbundle

我已经安装了FosUserBundle,我想用PhpUnit进行测试,检查用户是否可以登录,但我的代码一定是错的,因为我无法重定向。

我的代码:

public function testLogin()
    {
        $client = static::createClient();
        $crawler = $client->request('GET', '/crm/login');
        $buttonCrawlerNode = $crawler->selectButton('_submit');
        $form = $buttonCrawlerNode->form();
        $data = array('_username' => 'root','_password' => 'toor');
        $client->submit($form,$data);
        $crawler = $this->client->followRedirect();
        $crawler = $client->request('GET', '/crm/home');
    }

1 个答案:

答案 0 :(得分:1)

您似乎正在使用client

的2个不同实例
public function testLogin()
{
    $client = static::createClient();
    $crawler = $client->request('GET', '/crm/login');
    $buttonCrawlerNode = $crawler->selectButton('_submit');
    $form = $buttonCrawlerNode->form();
    $data = array('_username' => 'root','_password' => 'toor');
    $client->submit($form,$data);

    //here you're using $this->client not $client
    $crawler = $this->client->followRedirect();
    $crawler = $client->request('GET', '/crm/home');
}