我的黄瓜测试失败了,没有填写表格

时间:2012-10-02 16:04:51

标签: ruby-on-rails cucumber factory-bot

我对rails非常陌生,甚至更新于BDD。我试图坚持DRY方法,但有些事情不在这里..

FEATURE

  Scenario: Add a new vehicle with valid data
    Given I exist as a user
      And I am logged in
      And I am on the Create New Vehicle page
    When I fill out the form with the following attributes:
      | vehicle_make    | DODGE       |
      | vehicle_model   | RAM 1500    |
      | vehicle_year    | 2005        |
      | vehicle_engine  | 5.7L        |
      | vehicle_color   | Black       |
      And I click the Create Vehicle button
    Then I should see Vehicle was successfully created.

STEP DEFINITION

    ## this is in common_steps.rb

    When /^I fill out the form with the following attributes:$/ do |table|
      puts table.rows_hash
      criteria = table.rows_hash.each do |field, value|
        fill_in field.to_sym, :with => value
      end
    end
    When /^I click the (.*?) button$/ do |button|
      click_button button
    end

测试结果

## seems that its not filling the form out as im getting
## "cannot be blank" validation errors

  Scenario: Add a new vehicle with valid data               # features/vehicles/vehicle_new.feature:12
    Given I exist as a user                                 # features/step_definitions/user_steps.rb:42
    And I am logged in                                      # features/step_definitions/common_steps.rb:18
    And I am on the Create New Vehicle page                 # features/step_definitions/common_steps.rb:22
    When I fill out the form with the following attributes: # features/step_definitions/common_steps.rb:33
      {"vehicle_make"=>"DODGE", "vehicle_model"=>"RAM 1500", "vehicle_year"=>"2005", "vehicle_engine"=>"5.7L", "vehicle_color"=>"Black"}
      | vehicle_make   | DODGE    |
      | vehicle_model  | RAM 1500 |
      | vehicle_year   | 2005     |
      | vehicle_engine | 5.7L     |
      | vehicle_color  | Black    |
    And I click the Create Vehicle button                   # features/step_definitions/common_steps.rb:39
    Then I should see Vehicle was successfully created.     # features/step_definitions/common_steps.rb:47
      expected there to be content "Vehicle was successfully created." in "GasLoggr | Create New Vehicle\n  \n      \n          Gasloggr\n          \n            \n            \n            \n          \n              Welcome Back Test User\n              \n            Home\n    About\n\n        \n      \n    \n  \n    Vehicles\n        Fillups\n        Edit Profile\n    Logout\n        \n\n  \n    \n      \n        \n          Create New Vehicle\n      \n      \n        \n          \n    NAVIGATION\n            Vehicles\n            Fillups\n            Edit Profile\n    \n        \n        \n          \n          \n\t\n\t\t\n\t\t\t\n    \n      4 errors prohibited this vehicle from being saved:\n\n      Color can't be blank\n        Engine can't be blank\n        Model can't be blank\n        Year can't be blank\n      \n  \n      Make\n      \n        \n    \n    \n      Model\n      \n        \n      \n    \n    \n      Year\n      \n        \n      \n    \n    \n      Engine\n      \n        \n      \n    \n    \n      Color\n      \n        \n      \n    \n    \n      Image\n      \n        \n    \n    \n      Description\n      \n        \n    \n    \n      Cancel\n    \n  \n\t\t\n\t\t\n\t\t\tLorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod\n\t\t\ttempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,\n\t\t\tquis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo\n\t\t\tconsequat. Duis aute irure dolor in reprehenderit in voluptate velit esse\n\t\t\tcillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non\n\t\t\tproident, sunt in culpa qui officia deserunt mollit anim id est laborum.\n\t\t\n\t\n\n        \n      " (RSpec::Expectations::ExpectationNotMetError)
      ./features/step_definitions/common_steps.rb:48:in `/^I should see (.*?)$/'
      features/vehicles/vehicle_new.feature:23:in `Then I should see Vehicle was successfully created.'

从我能说的一切来看,步骤定义不是填写表单,但是表格值根据结果中的这一行确实存在.. {"vehicle_make"=>"DODGE", "vehicle_model"=>"RAM 1500", "vehicle_year"=>"2005", "vehicle_engine"=>"5.7L", "vehicle_color"=>"Black"}

2 个答案:

答案 0 :(得分:2)

问题在于以下几行:

fill_in field.to_sym, :with => value

特别是.to_sym导致它没有将正确的值传递给表单字段。删除.to_sym并将其删除:

fill_in field, :with => value

修复了问题,我的测试现在正在通过,同时坚持DRY方法

答案 1 :(得分:0)

Scenario: Add a new vehicle with valid data
    Given I exist as a user
    And I am logged in
    And I am on the Create New Vehicle page
    When I fill out the form with the following attributes:
      | attribute_name  | attribute_name | // write attribute name here 
      | vehicle_make    | DODGE          |
      | vehicle_model   | RAM 1500       |
      | vehicle_year    | 2005           |
      | vehicle_engine  | 5.7L           |
      | vehicle_color   | Black          |
    And I click the Create Vehicle button
    Then I should see Vehicle was successfully created.

例如

| name      | display_name | // name and display_name it's a attribute name
| Ollie     | ollie        |
| David     | player       |
| Victoria  | Victoria     |