使用Cucumber和Capybara的Factory Girl,我如何填充表格?

时间:2011-02-03 21:37:45

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

我有这个:

Scenario: Login
  Given a user exists with first_name: "Fred"
  When I am on the home page
  And I fill in "email" with the user: "Fred"
  And I fill in "password" with the user: "Fred"
  And I submit the form
  Then I should see "Welcome"

问题是如何使用Factory Girl生成的用户的电子邮件填充email字段?电子邮件必须是唯一的,因此它被定义为序列:

sequence(:email) {|n| "joeschmoe#{n}@example.com"}

我也在使用pickle,但fill in没有任何步骤。如何通过工厂创建用户并使用相同的用户信息填写登录表单?

2 个答案:

答案 0 :(得分:1)

你需要为它添加自己的步骤。

When /^(?:|I )fill in "([^"]*)" with the #{capture_model}(?: within "([^"]*)")?$/ do |field, a, selector|

  with_scope(selector) do
    fill_in(field, :with => model!(a).send(field))
  end
end

答案 1 :(得分:1)

我把它更改为:

Scenario: Login
  Given a user exists with email: "fred@example.com", plain_password: "password", plain_password_confirmation: "password"
  And I am on the home page
  When I login with "fred@example.com", "password"
  And I submit the form
  Then I should see "Welcome"

使用以下步骤定义:

When(/^I login with "(\S+)", "(.*)"$/) do |email, password|
  fill_in 'username', :with => email
  fill_in 'plain_password', :with => password
end

# Can only be executed with Selenium or Envjs drivers
When /^I submit the form$/ do
  page.evaluate_script("document.forms[0].submit()")
end

它有效!