黄瓜似乎跳过给定的步骤

时间:2011-08-10 03:32:15

标签: ruby-on-rails tdd cucumber bdd

最好的我可以告诉黄瓜只在这两种情况之间点击数据库一次,但是它会在场景之间清除数据库。

功能:

Feature: a new user vists the site and signs up
    in order to get new users
    when an unlogged in users comes to the website
    then they should see the sign-up dialog
    and be able to signup for the website 

    Background:
        Given I have at least one deal

    Scenario: a new user is asked to signup
        Given I am on show deal
        Then I should see "New Here?"

    @javascript
    Scenario: new user signup failure
        Given I am on show deal
        When I fill in "consumer[user_attributes][email]" with "test@test.com"
        And I press "consumer_submit"
        Then I should see "1 error prohibited"

步骤定义:

Given /^I have at least one deal$/ do
  Deal.create copy:'Example Deal Copy', copy_header:'Example Deal Header', copy_subheader:'Example Deal Subheader' if Deal.all.size == 0
end

结果:

Background:                      # features/new_user_signup.feature:7
    Given I have at least one deal # features/step_definitions/new_user_signup_steps.rb:1

  Scenario: a new user is asked to signup # features/new_user_signup.feature:10
    Given I am on show deal               # features/step_definitions/web_steps.rb:44
    Then I should see "New Here?"         # features/step_definitions/web_steps.rb:105

  @javascript
  Scenario: new user signup failure                                        # features/new_user_signup.feature:15
    Given I am on show deal                                                # features/step_definitions/web_steps.rb:44
      Couldn't find Deal with ID=1 (ActiveRecord::RecordNotFound)
      ./app/controllers/deals_controller.rb:17:in `show'
      <internal:prelude>:10:in `synchronize'
      ./features/step_definitions/web_steps.rb:45:in `/^(?:|I )am on (.+)$/'
      features/new_user_signup.feature:16:in `Given I am on show deal'
    When I fill in "consumer[user_attributes][email]" with "test@test.com" # features/step_definitions/web_steps.rb:60
    And I press "consumer_submit"                                          # features/step_definitions/web_steps.rb:52
    Then I should see "1 error prohibited"                                 # features/step_definitions/web_steps.rb:105

Failing Scenarios:
cucumber features/new_user_signup.feature:15 # Scenario: new user signup failure

无论我放在第二位的情况都会给出ActiveRecord错误。为什么第二种情况下数据库中没有记录?

1 个答案:

答案 0 :(得分:1)

现在我知道你如何映射“show deal”我很想说问题是Deal实例可能存在,但它的id不等于1.你能检查一下吗?

这里有一个提示:当你在路径中定义路径时.rb,你可能会这样做:

when /the edit deal page/
edit_deal_path(Deal.first)

甚至是这样:

when /the deal page for deal named ".*"/
        deal_name = page_name.scan(/".*"/).first.gsub("\"", '') 
        deal = Deal.find_by_name(deal_name)
        deal_path(deal)

只要你已经定义了这样的“我在”网站:

Given /^(?:|I )am on (.+)$/ do |page_name|
  visit path_to(page_name)
end

它比“deal / 1”好得多:)