gpt4 book ai didi

ruby-on-rails - Capybara、Devise、CanCan 和 RSpec 集成测试 : valid sign in 302 redirects to example. com

转载 作者:行者123 更新时间:2023-12-04 07:30:00 25 4
gpt4 key购买 nike

更新:请参阅文章末尾了解规范现在如何工作,因为我在规范/请求而不是规范/ Controller 中有我的规范。仍然想知道如何获得有效的登录用户以与我的 Controller 进行集成测试。

我是第一次使用 Devise 和 CanCan,在进行最基本的集成测试时遇到了困难,我正在验证登录用户是否......好吧......已登录。我已阅读处理 Devise 和 RSpec 集成测试的无数帖子和答案(即通过直接通过 https://github.com/plataformatec/devise/wiki/How-To%3a-Test-with-Capybarahttp://schneems.com/post/15948562424/speed-up-capybara-tests-with-deviseCapybara, RSpec and Devise: any way to make integration tests faster by circumventing slow login and setting session directly? 访问 session 来加快它们的速度,但我什至无法获得按预期工作的标准帖子和我很困惑:

  1. 开发环境运行良好(可以登录并重定向到正确的页面,导航登录链接更改为注销等)。
  2. test.log 在执行 302 重定向到 http://www.example.com 之前表明没有问题而不是我的 root_path。

由于大量用户遇到类似问题并采用不同的解决方案,显然我不是唯一遇到此问题的人,因为 session 等的各个方面在不同情况下不可用。

精简测试:

subject { page }

describe 'should be able to log in' do
before do
visit '/users/sign_in'
user = FactoryGirl.create(:admin)
#user.add_role :admin
fill_in 'Username', with: user.username
fill_in 'Password', with: user.password
click_on 'Sign in'
end
it { should have_link 'Logout' }
end
...

相关的log.test输出:

 Started POST "/users/sign_in" for 127.0.0.1 at 2012-11-06 17:31:10 -0800
Processing by Devise::SessionsController#create as HTML
Parameters: {"utf8"=>"✓", "user"=>{"username"=>"username1", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"Sign in"}
[1m[36mUser Load (0.1ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."username" = 'username1' LIMIT 1[0m
[1m[35m (0.0ms)[0m SAVEPOINT active_record_1
[1m[36m (0.0ms)[0m [1mRELEASE SAVEPOINT active_record_1[0m
[1m[35m (0.0ms)[0m SAVEPOINT active_record_1
[1m[36m (0.1ms)[0m [1mUPDATE "users" SET "last_sign_in_at" = '2012-11-07 01:31:10.722712', "current_sign_in_at" = '2012-11-07 01:31:10.722712', "last_sign_in_ip" = '127.0.0.1', "current_sign_in_ip" = '127.0.0.1', "sign_in_count" = 1, "updated_at" = '2012-11- 07 01:31:10.723051' WHERE "users"."id" = 1[0m
[1m[35m (0.0ms)[0m RELEASE SAVEPOINT active_record_1
Redirected to http://www.example.com/
Completed 302 Found in 12ms (ActiveRecord: 0.0ms)

精简了 routes.rb:

authenticated :user do
root :to => 'home#index'
end
root :to => "home#index"
devise_for :users
resources :users

精简能力:

类(class)能力 包括 CanCan::Ability

 def initialize(user)
user ||= User.new # guest user (not logged in)
if user.has_role? :admin
can :manage, :all
end
...

为了彻底,当前的 Gemfile:

source 'https://rubygems.org'

gem 'rails', '3.2.8'
gem "bootstrap-sass", "~> 2.1.0.1"
gem 'faker', '1.1.2'
gem 'will_paginate', '~> 3.0.3'
gem 'bootstrap-will_paginate', '~> 0.0.9'
gem 'jquery-rails'
gem 'client_side_validations'
gem 'thin'
gem "devise", "~> 2.1.2"
gem "cancan", "~> 1.6.8"
gem "rolify", "~> 3.2.0"
gem "simple_form", "~> 2.0.4"

group :assets do
gem 'sass-rails', '~> 3.2.3'
gem 'coffee-rails', '~> 3.2.1'
gem 'uglifier', '>= 1.0.3'
end

group :development, :test do
gem 'sqlite3', '1.3.5'
gem "rspec-rails", "~> 2.11.4"
gem 'guard-rspec', '1.2.1'
#gem 'listen', github: 'guard/listen', branch: 'polling/double'
gem 'guard-spork'
gem 'guard-cucumber'
gem 'spork', '0.9.2'
gem "factory_girl_rails", ">= 4.1.0"
end

group :development do
gem 'annotate', '2.5.0'
gem "quiet_assets", ">= 1.0.1"
end

group :test do
gem 'capybara', ">= 1.1.2"
gem "email_spec", ">= 1.2.1"
gem "cucumber-rails", ">= 1.3.0", :require => false
gem "database_cleaner", ">= 0.9.1"
gem "launchy", ">= 2.1.2"
gem 'rb-fsevent', '0.9.1', :require => false
gem 'growl', '1.0.3'
gem 'terminal-notifier-guard'
end

group :production do
gem 'pg', '0.12.2'
end

还有另一个用户在执行此基本任务时遇到了问题,但事实证明这只是一个语法错误(请参阅 Login integration test with rspec/devise)所以我确定我只是遗漏了一些非常明显的东西。

更新:

你一定是在开玩笑吧。离开它几个小时后,由于某种原因,我发现我在 spec/controllers 中有这个 spec,当简单地将它切换到 spec/requests 时,一切都变绿了。

我引用:

If you are using Rails, put your Capybara specs in spec/requests or spec/integration.

发现于 https://github.com/jnicklas/capybara#readme

但是,我仍然想知道如何针对 future 的需求在需要登录用户的 Controller 上进行集成测试....对于过去的其他非 Devise 应用程序,这一直很简单如下所示:

describe SomeController do

let(:user) { FactoryGirl.create(:user) }

before { valid_sign_in user }

...

我的 helper 在哪里

  def valid_sign_in(user)
visit signin_path
fill_in "Email", with: user.email
fill_in "Password", with: user.password
uncheck(:remember_me)
click_button "Sign In"

# Sign in when not using Capybara as well.
cookies[:remember_token] = user.remember_token
end

最佳答案

有两个问题在起作用:

<强>1。 rspec/controller 与 rspec/requests

我试图在 spec/controller 下进行集成测试。根据 https://github.com/jnicklas/capybara#readme ,您需要将 Capybara 规范放在 spec/requests 或 spec/integration 中。根据 https://stackoverflow.com/a/5803121/9344 :

A request spec is a thin wrapper around ActionDispatch::IntegrationTest, which doesn't work like controller specs (which wrap ActionController::TestCase). Even though there is a session method available, I don't think it is supported (i.e. it's probably there because a module that gets included for other utilities also includes that method).

最初这可以通过 Devise 通过辅助方法解决,例如:

# file: spec/requests_helper.rb
def login(user)
post_via_redirect user_session_path, 'user[email]' => user.email, 'user[password]' => user.password
end

但是我已经实现的官方/最新解决方案(但请参阅下面第二点了解为什么它没有解决我的问题),可以在 https://github.com/plataformatec/devise/wiki/How-To:-Test-with-Capybara 找到。

<强>2。 Devise::HelperTests 用于 Controller / View 测试,而不是集成测试。

如果我想在 Controller 上做一些测试,我可以使用 Devise::TestHelpers(通过 spec_helper.rb 引用),但即便如此我也无法获得 Capybara 集成测试的功能,因为Devise::TestHelpers 是专门为 Controller / View 测试而不是集成测试编写的。参见 rspec & devise test helpers .

总而言之,我想在 Controller 测试中做的事情无法完成,因为它实际上是一个集成测试,并且 session 等在规范/ Controller 中不可用。但是,有一些 Devise 测试助手可用于帮助进行 Controller / View 测试。

更新

很久以后我重新访问了这个,现在已经让 Warden 助手在我的集成测试中运行。欢呼!这在我的 448 次测试中减少了 8-10 秒。诀窍是 https://github.com/plataformatec/devise/wiki/How-To:-Test-with-Capybara 上的新条目.具体来说:

Capybara-Webkit

If you have trouble using Warden's login_as method with the capybara-webkit driver, try setting run_callbacks to false in the login_as options struct

user = Factory.create(:user) 
login_as(user, :scope => :user,
:run_callbacks => false)

总而言之,每次我需要一个经过身份验证的用户进行集成测试时,我不必进行实际的表单填写和发布,现在我只需要 login_as(user, :scope => :user, :run_callbacks => false)(当然重构为辅助方法)。

关于ruby-on-rails - Capybara、Devise、CanCan 和 RSpec 集成测试 : valid sign in 302 redirects to example. com,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13262299/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com