gpt4 book ai didi

php - 您可以将场景称为 Behat 3 中的步骤吗?

转载 作者:可可西里 更新时间:2023-11-01 13:53:55 26 4
gpt4 key购买 nike

在编写功能测试时,有些部分会经常重复。例如用户登录:

I go to "/login"
I fill in "login" with "username"
I fill in "password" with "password"
I press "Login"

我想将这些步骤定义为:

Given I am logged in as "userA" 

现在在 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"'),
);

Behat 3 是否仍然鼓励这种行为?有一个更好的方法吗?

最佳答案

这称为步骤执行链接,它已在 Behat 3 中删除。Here是 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
}
}

另一个关于 Behat 的背景、步骤和语言的精彩对话是 here

关于php - 您可以将场景称为 Behat 3 中的步骤吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27666321/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com