使用设计和黄瓜测试登录 [英] Testing login with devise and cucumber
问题描述
我正在尝试使用 Cucumber 测试登录功能.我的文件 users_steps.rb 包含
I am trying to test the login functionality with Cucumber. My file users_steps.rb contains
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
但是,此测试无法让用户登录.我已检查用户是否已正确创建,但在填写表单后,我被重定向到登录页面.
However this test fails to sign the user in. I have checked that the user is correctly created but after filling in the form I am redirected to the login page.
我正在使用水豚.我错过了什么?
I am using capybara. What am I missing?
推荐答案
你可能想用:
1.在位于 step_definitions 的 user_steps.rb 文件中:
1. In a user_steps.rb file located in step_definitions:
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
在您的功能中测试身份验证:
In your feature to test authentication:
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 中更改登录页面的路径
Do not forget to change the path to your login page in the support/paths.rb
when /the login page/
user_session_path
这里我的路径是使用设计默认设置.您可以使用 rake routes
来发现您的登录路径.
Here my path is using devise default setting. You can use rake routes
to discover your login path.
您可能需要更改登录"、登录成功"中的文本以匹配您的页面.我的假设是您使用的是 cucumber+capybara+devise 的默认配置.
You might have to change the text in the "Sign in", "Signed in successfully" to match your page. My assumptions here is that your are using the default config for cucumber+capybara+devise.
这篇关于使用设计和黄瓜测试登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!