环境
Rails 4.2.0
ruby-2.2.1 [ x86_64 ]
devise 3.4.1
rspec-core 3.2.2
rspec-rails 3.2.1
在我的/spec/rails_helper.rb 我已包含 设计 标记为
type: :controller
的规范文件的助手和 type: :request
规范/rails_helper.rb
ActiveRecord::Migration.maintain_test_schema!
RSpec.configure do |config|
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = false
config.before(:suite) do
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with(:truncation)
end
config.before(:suite) do
begin
DatabaseCleaner.start
FactoryGirl.lint
ensure
DatabaseCleaner.clean
end
end
config.around(:each) do |example|
DatabaseCleaner.cleaning do
example.run # ==================> L-60
end
end
config.include FactoryGirl::Syntax::Methods
# RSpec Rails can automatically mix in different behaviours to your tests
# based on their file location, for example enabling you to call `get` and
# `post` in specs under `spec/controllers`.
#
# You can disable this behaviour by removing the line below, and instead
# explicitly tag your specs with their type, e.g.:
#
# RSpec.describe UsersController, :type => :controller do
# # ...
# end
#
# The different available types are documented in the features, such as in
# https://relishapp.com/rspec/rspec-rails/docs
config.infer_spec_type_from_file_location!
config.include Devise::TestHelpers, type: :controller
config.include Devise::TestHelpers, type: :request
end
有了该配置,
type: controller
规范运行良好。但是在运行 type: request
时我收到以下错误: Failure/Error: Unable to find matching line from backtrace
NoMethodError:
undefined method `env' for nil:NilClass
# /home/.rvm/gems/ruby-2.2.1@myapp/gems/devise-3.4.1/lib/devise/test_helpers.rb:24:in `setup_controller_for_warden'
# ./spec/rails_helper.rb:60:in `block (3 levels) in <top (required)>'
# /home/.rvm/gems/ruby-2.2.1@simplyhomeapp/gems/database_cleaner-1.4.1/lib/database_cleaner/generic/base.rb:15:in `cleaning'
# /home/.rvm/gems/ruby-2.2.1@simplyhomeapp/gems/database_cleaner-1.4.1/lib/database_cleaner/base.rb:92:in `cleaning'
# /home/.rvm/gems/ruby-2.2.1@simplyhomeapp/gems/database_cleaner-1.4.1/lib/database_cleaner/configuration.rb:86:in `block (2 levels) in cleaning'
# /home/.rvm/gems/ruby-2.2.1@simplyhomeapp/gems/database_cleaner-1.4.1/lib/database_cleaner/configuration.rb:87:in `call'
# /home/.rvm/gems/ruby-2.2.1@simplyhomeapp/gems/database_cleaner-1.4.1/lib/database_cleaner/configuration.rb:87:in `cleaning'
# ./spec/rails_helper.rb:59:in `block (2 levels) in <top (required)>'
https://github.com/plataformatec/devise/blob/master/lib/devise/test_helpers.rb#L24正在关注
def setup_controller_for_warden #:nodoc:
@request.env['action_controller.instance'] = @controller # ==================> L-24
end
我知道 @request 实例不适用于 :request 类型规范,因此会出现错误。
使用 Devise 时,是否有任何帮助程序可用于在 :request type specs 中登录用户?
我发现了一个类似的问题 https://github.com/plataformatec/devise/issues/1114 , reply建议如下:
If you're doing integration tests, make sure to sign in your user in the tradicional way, by filling the sign in form and submitting.
但我想绕过需要登录用户的规范的实际登录。
谢谢。
最佳答案
虽然这里的流行答案(也复制在 Devise wiki 上)是可以的,但最简单的方法是:
规范/rails_helper.rb
RSpec.configure do |config|
# ...
config.include Devise::Test::IntegrationHelpers, type: :request
end
只需使用
sign_in
在您的请求规范中。这相当于声明 include Devise::Test::IntegrationHelpers
在系统/功能规范或 Rails 系统/ Controller 测试中。引用
Devise wiki: How to sign in and out a user in request type specs - Simple approach
关于devise 和 rspec-rails - 如何在请求类型规范中登录用户(用类型 : :request)? 标记的规范,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29509584/