【问题标题】:How to write cucumber for rails (Best practice). Feature and Steps如何为rails编写黄瓜(最佳实践)。功能和步骤
【发布时间】:2012-11-21 00:12:11
【问题描述】:

我目前正在尝试学习黄瓜以及如何正确利用它。在搜索最佳实践时,描述了大多数旧方法,但我还没有真正找到好的指南。 我阅读了有关新方法的信息,但在最佳实践方面存在一些问题。

以下是我一直在研究的一些基本黄瓜场景。

Scenario: Unsuccessful login
    Given a user has an account
    When the user tries to log in with invalid information
    Then the user should see an log in error message

Scenario: Successful login
   Given a user has an account
   When the user logs in
   Then the user should see an log in success message
   And the user should see a sign out link

Scenario: Successful logout
   Given a signed in user
   Then the user logs out
   And the user should see an log out success message

我想知道这样可以吗?如果我应该写成“我访问”或“用户访问”或“他访问”我有问题 基本上首选什么?

其次,我想知道我应该如何制定以下内容:

Scenario: Visit profile of user
  Given a user
  And a second user
  When the user visit the user profile
  Then the user should see the name of the user

Scenario: Visit profile of another user
  Given a user
  And a second user
  When the user visit the second users profile
  Then the user should see the name of the second user

这只是我放在一起的东西,但我觉得这不是最好的方法。 我在步骤定义文件中遇到问题。您将如何定义处理场景的步骤?我想写一些更笼统的东西,但也许真的不可能?我应该有一个@second_user 属性还是你有什么建议?

def user
  @user ||= FactoryGirl.create :user
end

Given /^a signed in user$/ do
  user
  sign_in(@user.email, @user.password)
end

Given /^a user has an account$/ do
  user
end


When /^the user logs in$/ do
  sign_in(@user.email, @user.password)
end

When /^the user logs out$/ do
  click_link ('Sign out')
end

When /^the user tries to log in with invalid information$/ do
  sign_in("incorrect-email", "incorrect-password")
end

Then /^the user should see a sign out link$/ do
  page.should have_link('Sign out')
end

Then /^the user should see an log in success message$/ do
  should have_success_message('Signed in successfully.')
end

When /^the user should see an log out success message$/ do
  should have_success_message('Signed out successfully.')
end

Then /^the user should see an log in error message$/ do
  should have_error_message('Invalid email or password.')
end

感谢您帮助我!

【问题讨论】:

    标签: ruby-on-rails ruby rspec cucumber capybara


    【解决方案1】:

    如果我应该写成“我访问”或“用户访问”或“他访问”我有问题,基本上首选什么?

    我认为使用“当用户访问时”之类的内容会更通用,并且更具可读性,因为您不必一直想“谁是 '他'?”如果您正在阅读测试,真正重要的是您在所有文件中都遵守一些约定,这样您就不会感到困惑。

    关于您是否创建@second_user 的第二个问题,我认为您不应该,因为该用户不完全是您的场景的一部分,而是数据设置,我认为处理数据设置的最佳方式就是使用pickle with cucumber,它基本上可以让你在你的黄瓜中创建模型,而不必将它们作为变量保留,有一个great cast on RailsCast可以解释很多。

    所以我会用泡菜来做这个

    Scenario: Visit profile of another user
      Given a user exists with name: "Mike", username: "mike"
      And a signed in user
      When the user visits the profile of "mike"
      Then the user should see 'Mike'
    

    那么你可以定义

    When /^the user visits the profile of (.+)$/ do |username|
      visit("/#{username}") # I am assuming here usernames are unique and the profile url is "/:username"
    end
    

    【讨论】:

    • 谢谢。似乎没有办法在模型和黄瓜特征之间添加一些耦合。我希望在我的功能中没有任何此类信息,例如用户名:但似乎没有什么好的方法。
    • 不客气,我认为如果有耦合没有什么问题,因为这就是我们编写测试的原因。例如,如果您将用户模型更改为没有用户名字段,那么这个场景就会失败这是应该发生的事情,然后您将修复模型或更改场景。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-06
    • 2018-05-20
    • 2018-02-08
    • 1970-01-01
    相关资源
    最近更新 更多