我配置了 jenkins 以便为我的项目自动化测试。单元测试运行良好。现在我想用我的数据库做一些测试。例如登录测试:输入用户名、密码(提交表单)。你知道如何做到这一点吗?谢谢你。
这是我想做但不起作用的测试:获取主页,测试链接FAQ是否存在两次,点击第一个FAQ链接,测试链接RMK是否存在,点击登录,测试是否忘记密码链接是否存在,输入我的用户名和密码提交表单,获取个人资料页面,测试我的个人资料链接是否存在。
<?php
namespace RMK\SocialBundle\Tests\FunctionalTests\Controller\Website;
use RMK\SocialBundle\Controller\Website;
use Symfony\Component\DomCrawler\Crawler;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Bundle\FrameworkBundle\Controller\RedirectController;
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
use Symfony\Bundle\FrameworkBundle\Controller\ControllerNameParser;
class HomeControllerTest extends WebTestCase
{
public function testViewFAQ(){
$client = static::createClient();
$crawler = $client->request('GET', '/home');
$this->assertEquals(2, $crawler->filter('a:contains("FAQ")')->count());
$this->assertEquals(2, $crawler->filter('a:contains("FAQ")')->count());
$faqlink = $crawler->filter('a:contains("FAQ")')->eq(0)->link();
$crawler = $client->click($faqlink);
$this->assertEquals(1, $crawler->filter('a:contains("RMK")')->count());
$loginLink = $crawler->filter('a:contains("Login")')->eq(0)->link();
$crawler = $client->click($loginLink);
$this->assertEquals(1, $crawler->filter('a:contains("Forgot Password?")')->count());
$form = $crawler->selectButton('_submit')->form();
$form['_username'] = 'username';
$form['_password'] = 'test';
$crawler = $client->submit($form);
$crawler = $client->followRedirect();
$crawler = $client->request('GET', '/profile');
$this->assertEquals(1, $crawler->filter('a:contains("My Profile")')->count());
}
}
?>
最后一个断言失败了:断言 0 匹配预期失败 1. 当我放 var_dump(print_r($client->getResponse()->getContent())); 就在最后一个断言之前,我得到了这个:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="refresh" content="1;url=/home" />
<title>Redirecting to /home</title>
</head>
<body>
Redirecting to <a href="/home">/home</a>.
</body>
</html>bool(true)
我确定我错过了一些东西,但我不知道是什么。你有想法吗?谢谢。PS:我在 Ubuntu 12.04 上。