在步骤定义文件的某个位置,应使用
before
步进法:
Before do
include Authlogic::TestCase
activate_authlogic
end
您的用户工厂应如下所示:
Factory.define :user do |f|
f.sequence(:id) { |n| n }
f.sequence(:username) { |n| "test#{n}" }
f.password "secret"
f.password_confirmation { |u| u.password }
f.sequence(:email) { |n| "test#{n}@example.com" }
end
但您不需要使用工厂来创建用户。以下是我的注册方案和现有用户方案:
Scenario: Successful sign up
Given I am an anonymous user
And I am on the home page
When I follow "Join now!"
And I fill in "Username" with "test"
And I fill in "Email" with "test@mail.ru"
And I fill in "Password" with "secret"
And I fill in "Confirm Password" with "secret"
And I press "Create my account"
Then I should be logged in
Scenario Outline: Show Sign in or Sign out and Sign Up links
Given the following user records
| username | password |
| test | secret |
Given I am logged in as "<login>" with password "secret"
When I go to the home page
Then I should <action 1>
And I should <action 2>
Examples:
| login | action 1 | action 2 |
| test | see "Sign out" | see "My Profile" |
| | see "Sign in" | see "Sign up" |
工厂将在第二个场景中使用。对应步骤定义:
Given /^I am an anonymous user$/ do
!UserSession.find
end
Then /^I should be logged in$/ do
UserSession.find
end
Given /^the following (.+) records$/ do |factory, table|
table.hashes.each do |hash|
Factory(factory, hash)
end
end
Given /^I am logged in as "([^\"]*)" with password "([^\"]*)"$/ do |username, password|
unless username.blank?
visit signin_url
fill_in "Username", :with => username
fill_in "Password", :with => password
click_button "Sign In"
end
end