Rails,Devise,Rspec:未定义的方法'sign_in' [英] Rails, Devise, Rspec: Undefined method 'sign_in'
问题描述
我尝试过的东西:
- 我在Rspec.configure中包含测试助手。
- 使用Warden的login_as
- 清除Rails缓存。
- 摆脱Capybara看看是否导致问题
- 我没有在我的控制器规范中明确地设置会话(例如没有valid_session)
到目前为止,没有骰子。使用登录用户测试我的控制器需要做些什么?
错误消息:
OrderItemsController GET #index渲染:索引视图
失败/错误:sign_in:admin
NoMethodError:
未定义的方法`sign_in'用于#< RSpec :: ExampleGroups :: OrderItemsController_2 :: GETIndex:0x00000102c002d0>
#./spec/controllers/order_items_controller_spec.rb:6:in'block(2 levels)in< top(required)>'
控制器规格
require'spec_helper'
描述OrderItemsController do
before(:each)do
admin = create(:admin)
sign_in:admin
end
描述GET #index do
it呈现:index视图do
get:index
expect(response).to render_template:index
end
end
end
spec_helper.rb
require'rspec / rails'
require'capybara / rspec'
RSpec.configure do | config |
config.include ApplicationHelper
config.include ControllersHelper
config.include UsersHelper
config.include Devise :: TestHelpers,键入:controller
config。包括FactoryGirl :: Syntax :: Methods
end
Gemfile
group:development,:test do
gem'rspec-rails','〜>
gem'capybara'
gem'factory_girl_rails'
gem'faker'
gem'dotenv-rails'
gem'guard'
gem'guard-annotate'
gem'guard-rspec',require:false
gem'guard-livereload',require:false
gem'foreman'
end
工厂/ user.rb
FactoryGirl.define do
factory:user do
first {Faker :: Name.first_name}
last {Faker :: Name.last_name}
email {Faker :: Internet.email}
admin false
密码secrets1
password_confirmationsecrets1
confirmation_at Date.today
工厂:admin do
admin true
end
end
end
谢谢提前。
你最近像我一样升级到RSpec 3吗?这是来自 RSpec 3文档 :
自动添加元数据
3.0.0之前的RSpec版本会自动将元数据添加到基于
的基础上,文件系统。这对于新用户而言是混乱的,而对于一些老用户来说,这不是
。
在RSpec 3中,必须明确启用此行为:
#spec / rails_helper.rb
RSpec.configure do | config |
config.infer_spec_type_from_file_location!
end
由于这个假设行为在教程中非常流行,所以默认的
由rails生成的配置生成rspec:install启用此功能。
如果您遵循上述列举的规范目录结构,并且
配置为infer_spec_type_from_file_location !, RSpec将自动
包含每个类型的正确的支持功能。
添加该配置代码片段后,我不再需要指定规范类型(例如 type::controller
)。
I am trying to write Rspec tests in Rails, using Devise helper methods for signing in and out. The sign_in method is not working. However, it had been working earlier, before a slew of changes to the app.
Things I have tried:
- I am including the test helpers in Rspec.configure.
- Using Warden's login_as
- Clearing the Rails cache.
- Getting rid of Capybara to see if that were causing the issue
- I am not setting the session explicitly in my controller specs (e.g. no valid_session)
So far, no dice. What do I need to do differently to test my controllers with a signed-in user?
Error message:
OrderItemsController GET #index renders the :index view
Failure/Error: sign_in :admin
NoMethodError:
undefined method `sign_in' for # <RSpec::ExampleGroups::OrderItemsController_2::GETIndex:0x00000102c002d0>
# ./spec/controllers/order_items_controller_spec.rb:6:in `block (2 levels) in <top (required)>'
Controller Spec
require 'spec_helper'
describe OrderItemsController do
before (:each) do
admin = create(:admin)
sign_in :admin
end
describe "GET #index" do
it "renders the :index view" do
get :index
expect( response ).to render_template :index
end
end
end
spec_helper.rb
require 'rspec/rails'
require 'capybara/rspec'
RSpec.configure do |config|
config.include ApplicationHelper
config.include ControllersHelper
config.include UsersHelper
config.include Devise::TestHelpers, type: :controller
config.include FactoryGirl::Syntax::Methods
end
Gemfile
group :development, :test do
gem 'rspec-rails', '~> 3.0.0.beta'
gem 'capybara'
gem 'factory_girl_rails'
gem 'faker'
gem 'dotenv-rails'
gem 'guard'
gem 'guard-annotate'
gem 'guard-rspec', require: false
gem 'guard-livereload', require: false
gem 'foreman'
end
factories/user.rb
FactoryGirl.define do
factory :user do
first { Faker::Name.first_name }
last { Faker::Name.last_name }
email { Faker::Internet.email }
admin false
password "secrets1"
password_confirmation "secrets1"
confirmed_at Date.today
factory :admin do
admin true
end
end
end
Thanks in advance.
Did you recently upgrade to RSpec 3 like I did? This is from the RSpec 3 documentation:
Automatically Adding Metadata RSpec versions before 3.0.0 automatically added metadata to specs based on their location on the filesystem. This was both confusing to new users and not desirable for some veteran users.
In RSpec 3, this behavior must be explicitly enabled:
# spec/rails_helper.rb RSpec.configure do |config| config.infer_spec_type_from_file_location! end
Since this assumed behavior is so prevalent in tutorials, the default configuration generated by rails generate rspec:install enables this.
If you follow the above listed canonical directory structure and have configured infer_spec_type_from_file_location!, RSpec will automatically include the correct support functions for each type.
After I add that configuration snippet, I no longer have to specify the spec type (e.g. type: :controller
).
这篇关于Rails,Devise,Rspec:未定义的方法'sign_in'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!