黄瓜步骤定义失败

时间:2012-05-03 09:47:33

标签: ruby-on-rails-3 cucumber

我有一个失败的步骤定义。

从下面的功能我失败了'并且该功能有问题'(标题处未定义的局部变量)

Feature: Viewing issue
In order to view the issues for a feature
As a user
I want to see them on that feature's page
Background:    
    Given there is a release called "Confluence"
    And that release has a feature:
      | title                | description   |
      | Make it shiny!       | Gradients! Starbursts! Oh my! |
    And that feature has a issue:
      | title                | description   |
      | First Issue          | This is a first issue. |
    And I am on the homepage

  Scenario: Viewing issue for a given feature
    When I follow "Confluence"
    Then I should see "Standards compliance"
    When I follow "Standards compliance"
    Then I should see "First Issue" 
    And I should see "This is a first issue."

我如何为它们写一个步骤定义。

这就是我对功能定义所具有的功能,但它的功能很好,但我已经尝试对问题对象做同样的事情并且它不起作用

Given /^that release has a feature:$/ do |table|
  table.hashes.each do |attributes|
    @release.features.create!(attributes)
  end
end

1 个答案:

答案 0 :(得分:0)

作为最佳做法,您不应在步骤中使用实例变量。它使它们彼此依赖。

我会像这样编写你的步骤(在psuedo-ish代码中):

Given there is a release called <name>
    # create model

Given release <name> has a feature <table>
    release = Release.find_by_name(<name>)
    # rest of your code here is fine, but reference 'release' instead of '@release'

Given release <name>'s <feature_name> has issues <table>
    release = Release.find_by_name(<name>)
    feature = release.features.find_by_name(<feature_name>)
    table.hashes.each do |attributes|
       # create issue
    end

然后你的Cucumber功能会更好地读取:

Given there is a release called "Confluence"
And release "Confluence" has a feature:
  | title                | description   |
  | Make it shiny!       | Gradients! Starbursts! Oh my! |
And release "Confluence"'s "Make it shiny!" feature has issues
  | title                | description   |
  | First Issue          | This is a first issue. |