2016-02-28 147 views
1

我已經安裝了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'); 
    } 
+2

2種不同的情況下,也許你使用的是不同的客戶端實例? '$ this-> client-> followRedirect();' - >'$ client-> followRedirect();' – 1ed

回答

1

看來你正在使用的client

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'); 
} 
相關問題