【问题标题】:How do I avoid ambiguous step definitions in Cucumber?如何避免 Cucumber 中的步骤定义不明确?
【发布时间】:2012-10-03 13:14:08
【问题描述】:

我遇到了模棱两可的步骤定义错误。以下场景存在于不同的功能文件中,并且具有相同的步骤:Then I should see my profile page。如何避免这种情况发生?

# authentication.feature
Scenario: Successful sign in
  Given I visit the sign in page
  When I submit valid sign in information
  Then I should see my profile page

# signing_up.feature
Scenario: Successful sign up
  Given I visit the sign up page
  When I submit valid information
  Then I should see my profile page

# authentication_steps.rb
...
Then /^I should see my profile page$/ do
  page.current_path.should == user_path(@user)
end

# signing_up.feature
...
Then /^I should see my profile page$/ do
  page.current_path.should == user_path(@user)
end

我也遇到了这个错误消息的问题:

Scenario: Unsuccessful sign up
  ...
  Then I should see an error message

Scenario: Unsuccessful login
  ...
  Then I should see an error message

【问题讨论】:

  • 请显示这些步骤定义。如果它们相同,您只需要一个。如果他们做不同的事情,那么他们应该有不同的名字
  • @IsmaelAbreu,我添加了步骤定义!

标签: ruby-on-rails ruby testing cucumber bdd


【解决方案1】:

为什么不只保留 authentication_steps.rb 中的那个呢?我认为您可以使用任何功能文件中的所有步骤

【讨论】:

  • Ismael 是对的,无需重复您的步骤定义。您应该以语义为标准对步骤进行分组。他也是对的,您可以在任何场景中使用任何步骤。
  • 我想通了。唯一的事情是,我对I should see my profile page 的步骤定义取决于一个实例变量。它看起来有点乱。我有一种方法可以将它传递给一个对象,这样我就可以将它作为一个局部变量实例来引用,让它依赖于 @user 的工作?
  • 是的,您可以有一个路由 /my_profile 重定向到登录的用户个人资料页面。或者您可以在这些步骤中指定参数。
  • When I submit valid information with username: "someusername"
  • Then I should see profile page of: "someusername" 这可能会在其他功能中派上用场
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-18
  • 1970-01-01
  • 2022-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多