用devise和黄瓜测试登录 [英] Testing login with devise and cucumber

查看:143
本文介绍了用devise和黄瓜测试登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用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.

我使用capybara。

I am using capybara. What am I missing?


推荐答案

您可能想要使用:

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中更改登录页面的路径

Do not forget to change the path to your login page in the support/paths.rb

when /the login page/
  user_session_path

这里我的路径是使用devise默认设置。您可以使用 rake routes 来发现您的登录路径。

Here my path is using devise default setting. You can use rake routes to discover your login path.

您可能需要更改在,已成功登录以匹配您的网页。我的假设这里是你使用的默认配置黄瓜+ capybara +设计。

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.

这篇关于用devise和黄瓜测试登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆