为什么我的Cucumber场景失败了"无效键:文本,应该是以下之一:text,..."?

时间:2014-08-21 11:57:52

标签: ruby-on-rails cucumber capybara

我有以下代码来验证表单是否只接受dd-mm-yyyy格式的日期值。但是,我收到了错误

  

无效的密钥:文本,应该是以下之一:text,:visible,:between,:count,:maximum,:minimum,:exact,:match,:wait

我认为这是由我的步骤定义中的fill_in arg2引起的,但我无法弄清楚实际上是什么错误。

功能文件:

Scenario Outline: Edit Person Validation
  Given I login
  And I edit a person
  When I enter <value> as <field>
  Then I should see <validity>

Scenarios: valid
| value         | field             | validity  |
| 20-05-2014    | person_startdate  | success   |
| JA-61-47-66-C | person_nino       | success   |

Scenarios: invalid
| value         | field             | validity  |
| 05-20-2014    | person_startdate  | failure   | 
| 2014-05-20    | person_startdate  | failure   | 
| 2014-20-05    | person_startdate  | failure   | 
| 20/05/2014    | person_startdate  | failure   | 
| 20-05/2014    | person_startdate  | failure   |
| 20/05-2014    | person_startdate  | failure   |
| 20/05-2014    | person_startdate  | failure   |
| 20'05'2014    | person_startdate  | failure   | 
| Today         | person_startdate  | failure   | 
| Tomorrow      | person_startdate  | failure   |  
| 2014          | person_startdate  | failure   |
| March         | person_startdate  | failure   | 
| 05-20         | person_startdate  | failure   |
| JH-22-43-61   | person_nino       | failure   |

步骤定义:

Given /^I login$/ do 
  login "*****", "*****"
end

Given /^I edit a person$/ do
  visit edit_person_path('1682')
end

When /^I enter (.+) as (.+)$/ do |arg1,arg2|
  fill_in arg2, with: arg1
  click_on 'Save'
end

Then /^I should see failure$/ do
  expect(page).to have_text('div', text: 'prohibited')
end

Then /^I should see success$/ do
  expect(page).to have_selector('h1', text: 'Your People')
end

def login(username, password)
  visit login_path
  @user = User.create(username: username, password: password)
  fill_in "username_or_email", with: @user.username
  fill_in "login_password", with: @user.password
  click_on "Log In"
end

1 个答案:

答案 0 :(得分:1)

我猜错误在

Then /^I should see failure$/ do
  expect(page).to have_text('div', text: 'prohibited')
end

这不是have_text的使用方式。做任何一件事

expect(page).to have_selector('div', text: 'prohibited')

或(只有当你不关心检查文本是否在div中时,可能你不会因为那不是一个非常具体的选择器)

expect(page).to have_text('prohibited')