我正在尝试使用Cucumber测试登录功能。 我的文件users_steps.rb包含
Given /^I am a user named "([^"]*)" with an email "([^"]*)" and password "([^"]*)"$/ do |name, email, password|
u = User.new(:name => name,
:email => email,
:password => password,
:password_confirmation => password)
u.skip_confirmation!
u.save!
end
When /^I sign in as "(.*)\/(.*)"$/ do |email, password|
#Given %{I am not logged in}
When %{I go to the sign in page}
And %{I fill in "user_email" with "#{email}"}
And %{I fill in "user_password" with "#{password}"}
And %{I press "Log Me In"}
end
Then /^I should be signed in$/ do
Then %{I should see "Sign out"}
end
Then /^I should be signed in$/ do
Then %{I should see "Sign out"}
end
Then /^I sign out$/ do
visit('/account/logout')
end
我的黄瓜情景是:
Scenario: User signs in successfully with email
Given I am not logged in
And I am a user named "foo" with an email "user@test.com" and password "please"
When I go to the sign in page
And I sign in as "user@test.com/please"
Then I should be signed in
When I return next time
Then I should be already signed in
然而,此测试无法签署用户。我已检查用户是否已正确创建,但在填写表单后,我被重定向到登录页面。
我正在使用水豚。我错过了什么?
答案 0 :(得分:2)
您可能想要使用:
1。在位于step_definitions中的user_steps.rb文件中:
Given /^a valid user$/ do
@user = User.create!({
:email => "minikermit@hotmail.com",
:password => "12345678",
:password_confirmation => "12345678"
})
end
Given /^a logged in user$/ do
Given "a valid user"
visit signin_url
fill_in "Email", :with => "minikermit@hotmail.com"
fill_in "Password", :with => "12345678"
click_button "Sign in"
end
在测试身份验证的功能中:
Scenario: Login
Given a valid user
When I go to the login page
And I fill in the following:
|Email|minikermit@hotmail.com|
|Password|12345678|
And I press "Sign in"
Then I should see "Signed in successfully."
不要忘记在support / paths.rb
中更改登录页面的路径when /the login page/
user_session_path
这里我的路径是使用设计默认设置。您可以使用rake routes
来发现您的登录路径。
您可能需要更改“登录”,“成功登录”中的文字以匹配您的网页。我的假设是你正在使用黄瓜+ capybara + devise的默认配置。