我正在尝试对表单进行功能测试,如下所示
//快乐的场景
public function testRegisterActionInvalidCase($password, $confPassword, $gender, $country, $city, $schoo, $schoolName, $shortName, $keyWords, $expectedMessage) {
$user = $this->_testUtilityService->getRandomEntity($this->_userService, 'FOS\UserBundle\Model\User');
$token = $user->getConfirmationToken();
$url = $this->generateUrl('teacher_register', array('token' => $token));
$crawler = $this->client->request('POST', $url);
$crawlerResponse = $this->client->getResponse();
//[ASSERTIONS] Page is loaded ok
$this->assertEquals(200, $crawlerResponse->getStatusCode());
//[ASSERTIONS] that the loaded page is the correct one
$this->assertContains($this->Translator->trans('Teacher Registration'), $this->client->getResponse()->getContent());
$form = $crawler->selectButton('Save')->form();
$confPassword = $password = $this->Faker->password();
if ($gender === 'GENDER') {
$gender = $this->Faker->randomElement([Gender::FEMALE, Gender::MALE]);
}
if ($gender === 'COUNTRY') {
$country = $this->_testUtilityService->getRandomEntity($this->_countryService, '\ITJari\GeneralBundle\Entity\Country');
}
if ($gender === 'CITY') {
$city = $this->_testUtilityService->getRandomEntity($this->_cityService, '\ITJari\GeneralBundle\Entity\City');
}
if ($gender === 'SCHOOL') {
$schoo = $this->_testUtilityService->getRandomEntity($this->_schoolService, '\ITJari\SchoolBundle\Entity\School');
}
// fill in the form to be submitted
$form["plainPassword['first']"] = $password;
$form["plainPassword['second']"] = $confPassword;
$form["gender"] = $gender;
$form["country"] = $country;
$form["city"] = $city;
$form["school"] = $schoo;
$form["school_name"] = $schoolName;
$form["short_name"] = $shortName;
$form["key_words"] = $keyWords;
$crawler = $this->client->submit($form);
//[ASSERTIONS] that the form submitted successfully
$this->assertTrue($crawler->filter('html:contains("Teacher has been created successfully")')->count() > 0);
}
//无效的案例 如何使用数据提供程序测试无效案例进行功能测试? 这是我的尝试:
/**
* @dataProvider provideDataForRegistrationActionInvalidCase
* @assertions Assert for exception asserted on "exception message"
*
*/
public function testRegisterActionInvalidCase($password, $confPassword, $gender, $country, $city, $schoo, $schoolName, $shortName, $keyWords, $expectedMessage) {
$user = $this->_testUtilityService->getRandomEntity($this->_userService, 'FOS\UserBundle\Model\User');
$token = $user->getConfirmationToken();
$url = $this->generateUrl('teacher_register', array('token' => $token));
$crawler = $this->client->request('POST', $url);
$crawlerResponse = $this->client->getResponse();
//[ASSERTIONS] Page is loaded ok
$this->assertEquals(200, $crawlerResponse->getStatusCode());
//[ASSERTIONS] that the loaded page is the correct one
$this->assertContains($this->Translator->trans('Teacher Registration'), $this->client->getResponse()->getContent());
$form = $crawler->selectButton('Save')->form();
$confPassword = $password = $this->Faker->password();
if ($gender === 'GENDER') {
$gender = $this->Faker->randomElement([Gender::FEMALE, Gender::MALE]);
}
if ($gender === 'COUNTRY') {
$country = $this->_testUtilityService->getRandomEntity($this->_countryService, '\ITJari\GeneralBundle\Entity\Country');
}
if ($gender === 'CITY') {
$city = $this->_testUtilityService->getRandomEntity($this->_cityService, '\ITJari\GeneralBundle\Entity\City');
}
if ($gender === 'SCHOOL') {
$schoo = $this->_testUtilityService->getRandomEntity($this->_schoolService, '\ITJari\SchoolBundle\Entity\School');
}
// fill in the form to be submitted
$form["plainPassword['first']"] = $password;
$form["plainPassword['second']"] = $confPassword;
$form["gender"] = $gender;
$form["country"] = $country;
$form["city"] = $city;
$form["school"] = $schoo;
$form["school_name"] = $schoolName;
$form["short_name"] = $shortName;
$form["key_words"] = $keyWords;
$crawler = $this->client->submit($form);
//[ASSERTIONS] that the form submitted successfully
$this->assertTrue($crawler->filter('html:contains("Teacher has been created successfully")')->count() > 0);
}
/**
* @description prepare data to be used as providers for test cases
* @author Mohamed Ragab Dahab <mdahab@treze.co.uk>
* @access public
*
* @covers TeacherService::submitSubscriptionFormData
*
* @return array arguments array passed to each test case scenario
*/
public function provideDataForRegistrationActionInvalidCase() {
[
//password
["password", "differentPassword", 'GENDER', 'COUNTRY', 'CITY', 'SCHOOL', 'my school name', 'my short name', 'my key words', 'password does not match'],
//Gender
["password", "password", 'invalid value', 'COUNTRY', 'CITY', 'SCHOOL', 'my school name', 'my short name', 'my key words', 'Gender invalid value'],
//Country
["password", "password", 'GENDER', 'invalid value', 'CITY', 'SCHOOL', 'my school name', 'my short name', 'my key words', 'Country Invalid value'],
//City
["password", "password", 'GENDER', 'COUNTRY', 'invalid value', 'SCHOOL', 'my school name', 'my short name', 'my key words', 'City Invalid value'],
//School
["password", "password", 'GENDER', 'COUNTRY', 'CITY', 'invalid value', 'my school name', 'my short name', 'my key words', 'School Invalid value'],
// School Name
["password", "password", 'GENDER', 'COUNTRY', 'CITY', 'SCHOOL', '112233Invalid Value', 'my short name', 'my key words', 'School name invalid value'],
// Short Name
["password", "password", 'GENDER', 'COUNTRY', 'CITY', 'SCHOOL', 'my school name', '112233Invalid Value', 'my key words', 'Short name invalid value'],
// Key Words
["password", "password", 'GENDER', 'COUNTRY', 'CITY', 'SCHOOL', 'my school name', 'my short name', 'TOO_MUCH_TEXT', 'Too much text'],
];
}
但是,我不知道如何将表单验证消息分配给使用数据提供程序
答案 0 :(得分:2)
请记住一些注意事项。
一般来说,测试看起来很复杂。我会把它分成2或3个测试。您可以在另一个测试中检查页面是否正常加载。此外,如果您需要令牌,用户或其他任何身份验证,我建议您制作某种隐式登录方法。
数据提供商看起来像这样:
/**
* Test description
* @dataProvider getSomeData
* @param array $data
*/
public function testSomething($data)
{
$test = $data['test'];
...
}
static function getSomeData()
{
return array(
array('test' => 'a'),
array('test' => 'b'),
array('test' => 'c'),
);
}
如果你用phpunit进行功能测试,那很好,但是真的考虑使用behat。一旦你习惯了,它就容易多了。