在编写功能测试时,一些部分会经常重复.例如,用户登录:
I go to "/login"
I fill in "login" with "username"
I fill in "password" with "password"
I press "Login"
Run Code Online (Sandbox Code Playgroud)
我想将这些步骤定义为:
Given I am logged in as "userA"
Run Code Online (Sandbox Code Playgroud)
现在在Behat 2.x上,我将在php中定义一个步骤:
return array(
new Step\Given('I go to "/login"'),
new Step\Then('I fill in "login" with "username"'),
new Step\Then('I fill in "password" with "password"'),
new Step\Then('I press "Login"'),
);
Run Code Online (Sandbox Code Playgroud)
Behat 3是否仍然鼓励这种行为?有一个更好的方法吗?
小智 7
这称为步执行链接,它在Behat 3中被删除.这是Behat创建者的原始答案.
如果您想使用MinkContext,只需在您的上下文中扩展它,或者如果您的代码更复杂,请使用合成模式.然后,您将能够直接调用负责这些步骤的方法,例如:
class FeatureContext extends MinkContext
{
/**
* @Given I am logged in as :user
*/
public function iAmLoggedInAsUser($user)
{
$this->visit('/login');
$this->fillField('login', 'username');
$this->fillField('password', 'password');
$this->pressButton('Login');
// make assertion to be sure user is logged in
}
}
Run Code Online (Sandbox Code Playgroud)
关于Behat的背景,步骤和语言的另一个伟大的对话就在这里
| 归档时间: |
|
| 查看次数: |
2067 次 |
| 最近记录: |