在 rails 应用程序中通过水豚身份验证
getting past authentication with capybara in rails application
我正在尝试在 rails 应用程序的 ruby 上使用水豚来进行一些内容测试,同时我正在使用 gem 设计来实现用户身份验证。我无法登录我的应用程序来执行我的测试用例。
最初,我的场景如下:
scenario "User arrives at main page" do
visit "purchase_orders#index"
page.should have_content("All")
# some more tests and checks
end
其中 purchase_orders#index 是经过身份验证的根目录,其中显示了用户的采购订单。
但是当我进行 运行 测试时,出现以下错误:
expected to find text "All" in "Log in to manage your orders * Email * Password Forgot your password? Remember me Sign up • Didn't receive confirmation instructions? About Us • Contact Us •
这告诉我它没有通过登录页面。接下来,我尝试在 运行 测试之前将以下内容添加到我的场景中,以使其登录:
visit "purchase_orders#index"
fill_in('Email', :with => 'username@gmail.com')
fill_in('Password', :with => 'password')
click_button('Log in')
其中用户名和密码是实际创建的帐户,但它再次失败并且没有通过登录页面。最后,我尝试添加一个 before(:each) 方法,如下所示,以尝试让用户登录测试用例:
before(:each) do
visit "users/sessions#new"
fill_in('Email', :with => 'nico.dubus17@gmail.com')
fill_in('Password', :with => 'password')
click_button('Log in')
end
同样,这对我不起作用。所以我的问题是:通过登录页面进入实际应用程序的最佳实践和语法是什么?我已经为此寻找文档和答案,但没有找到任何东西。
谢谢!
找到答案。我安装了 warden gem 到 (gem 'warden') 和 factory girl gem (gem "factory_girl_rails", "~> 4.0") 和 运行 捆绑安装。然后,我在 spec 文件夹的 factory.rb 文件中创建了一个带有 factory girl 的用户夹具:
# This is a user factory, to simulate a user object
FactoryGirl.define do
factory :user, class: User do
first_name "John"
last_name "Doe"
email "nico_dubus@hotmail.com"
encrypted_password "password"
end
end
在我的 rails 帮助程序文件中,我添加了这一行以便能够使用 FactoryGirl 的方法而无需每次都在 class 上调用它:
config.include FactoryGirl::Syntax::Methods
之后,我将这些行添加到水豚测试页的顶部:
include Warden::Test::Helpers
Warden.test_mode!
最后我建了一个用户对象在测试范围内使用:
user = FactoryGirl.build(:user)
而且每当我需要登录时,我都会使用监狱长的登录方法
login_as(user, :scope => :user)
瞧!
我正在尝试在 rails 应用程序的 ruby 上使用水豚来进行一些内容测试,同时我正在使用 gem 设计来实现用户身份验证。我无法登录我的应用程序来执行我的测试用例。
最初,我的场景如下:
scenario "User arrives at main page" do
visit "purchase_orders#index"
page.should have_content("All")
# some more tests and checks
end
其中 purchase_orders#index 是经过身份验证的根目录,其中显示了用户的采购订单。 但是当我进行 运行 测试时,出现以下错误:
expected to find text "All" in "Log in to manage your orders * Email * Password Forgot your password? Remember me Sign up • Didn't receive confirmation instructions? About Us • Contact Us •
这告诉我它没有通过登录页面。接下来,我尝试在 运行 测试之前将以下内容添加到我的场景中,以使其登录:
visit "purchase_orders#index"
fill_in('Email', :with => 'username@gmail.com')
fill_in('Password', :with => 'password')
click_button('Log in')
其中用户名和密码是实际创建的帐户,但它再次失败并且没有通过登录页面。最后,我尝试添加一个 before(:each) 方法,如下所示,以尝试让用户登录测试用例:
before(:each) do
visit "users/sessions#new"
fill_in('Email', :with => 'nico.dubus17@gmail.com')
fill_in('Password', :with => 'password')
click_button('Log in')
end
同样,这对我不起作用。所以我的问题是:通过登录页面进入实际应用程序的最佳实践和语法是什么?我已经为此寻找文档和答案,但没有找到任何东西。
谢谢!
找到答案。我安装了 warden gem 到 (gem 'warden') 和 factory girl gem (gem "factory_girl_rails", "~> 4.0") 和 运行 捆绑安装。然后,我在 spec 文件夹的 factory.rb 文件中创建了一个带有 factory girl 的用户夹具:
# This is a user factory, to simulate a user object
FactoryGirl.define do
factory :user, class: User do
first_name "John"
last_name "Doe"
email "nico_dubus@hotmail.com"
encrypted_password "password"
end
end
在我的 rails 帮助程序文件中,我添加了这一行以便能够使用 FactoryGirl 的方法而无需每次都在 class 上调用它:
config.include FactoryGirl::Syntax::Methods
之后,我将这些行添加到水豚测试页的顶部:
include Warden::Test::Helpers
Warden.test_mode!
最后我建了一个用户对象在测试范围内使用:
user = FactoryGirl.build(:user)
而且每当我需要登录时,我都会使用监狱长的登录方法
login_as(user, :scope => :user)
瞧!