我是黄瓜的新手,我找到以下代码片段来测试Devise登录功能。然而,似乎又缺少了一步,我没有找到任何解决方案:
Given /^that a confirmed user exists$/ do
pending # express the regexp above with the code you wish you had
end
以下代码:
Feature: Session handling
In order to use the site
As a registered user
I need to be able to login and logout
Background:
Given that a confirmed user exists
Scenario Outline: Logging in
Given I am on the login page
When I fill in "user_email" with "<email>"
And I fill in "user_password" with "<password>"
And I press "Sign in"
Then I should <action>
Examples:
| email | password | action |
| minimal@example.com | test1234 | see "Signed in successfully" |
| bad@example.com | password | see "Invalid email or password" |
Scenario: Logging out
Given I am logged in
When I go to the sign out link
Then I should see "Signed out successfully"
# Session
Given /^I am logged in$/ do
visit path_to('the login page')
fill_in('user_email', :with => @user.email)
fill_in('user_password', :with => @user.password)
click_button('Sign in')
if defined?(Spec::Rails::Matchers)
page.should have_content('Signed in successfully')
else
assert page.has_content?('Signed in successfully')
end
end
Factory.define :minimal_user, :class => User do |u|
u.username 'minimal'
u.email 'minimal@example.com'
u.password 'test1234'
u.password_confirmation 'test1234'
end
此处指向orginal code
的链接非常感谢你的帮助!!
答案 0 :(得分:2)
你的标题是“验证用户是否存在”,但这可能不是你需要做的 - 你的Given
步骤不需要断言某些东西是有用的,他们调用代码来创建您的方案的应用程序状态。当然,他们仍然在测试,因为他们仍然可以失败。
我认为你正在寻找这样的东西:
Given /^that a confirmed user exists$/ do
Factory.create(:minimal_user)
end
这将从您的工厂定义中创建并保存新的已确认用户,以便其余的方案可以继续。
答案 1 :(得分:1)
要完成Daniel的答案并且因为我启用了Devise确认模块,我应该在我的夹具中添加一行以告知用户已被确认。
例如:
Factory.define :minimal_user, :class => User do |u|
u.username 'minimal'
u.email 'minimal@example.com'
u.password 'test1234'
u.password_confirmation 'test1234'
u.confirmed_at 'here the date you want'
end
此外,您可以找到here的调试步骤非常有用。
希望它可以帮助一些人。
答案 2 :(得分:0)
(::) failed steps (::)
expected #has_content?("Signed in successfully") to return true, got false (RSpec::Expectations::ExpectationNotMetError)
./features/step_definitions/web_steps.rb:110:in `block (2 levels) in <top (required)>'
./features/step_definitions/web_steps.rb:14:in `with_scope'
./features/step_definitions/web_steps.rb:108:in `/^(?:|I )should see "([^"]*)"(?: within "([^"]*)")?$/'
features/authentication/session.feature:16:in `Then I should <action>'
undefined method `email' for nil:NilClass (NoMethodError)
./features/step_definitions/authentication_steps.rb:43:in `/^I am logged in$/'
features/authentication/session.feature:23:in `Given I am logged in'