Rails、Devise、Rspec:未定义的方法“sign_in" [英] Rails, Devise, Rspec: Undefined method 'sign_in'

查看:19
本文介绍了Rails、Devise、Rspec:未定义的方法“sign_in"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Rails 中编写 Rspec 测试,使用 Devise 辅助方法进行登录和注销.sign_in 方法不起作用.但是,在对应用程序进行大量更改之前,它一直可以正常工作.

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.

我尝试过的事情:

  • 我在 Rspec.configure 中包含了测试助手.
  • 使用 Warden 的 login_as
  • 清除 Rails 缓存.
  • 摆脱 Capybara 以查看是否是导致问题的原因
  • 我没有在控制器规范中明确设置会话(例如没有 valid_session)

到目前为止,还没有骰子.我需要做些什么不同的事情才能使用登录用户测试我的控制器?

So far, no dice. What do I need to do differently to test my controllers with a signed-in user?

错误信息:

 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)>'

控制器规格

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

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

宝石文件

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

工厂/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

提前致谢.

推荐答案

您最近是否像我一样升级到 RSpec 3?这是来自 RSpec 3 文档:

Did you recently upgrade to RSpec 3 like I did? This is from the RSpec 3 documentation:

自动添加元数据3.0.0 之前的 RSpec 版本自动将元数据添加到基于它们在文件系统上的位置.这对新用户来说既令人困惑又不适合一些资深用户.

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.

在 RSpec 3 中,必须显式启用此行为:

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

由于这种假定的行为在教程中如此普遍,因此默认由 rails generate rspec:install 生成的配置启用此功能.

Since this assumed behavior is so prevalent in tutorials, the default configuration generated by rails generate rspec:install enables this.

如果您遵循上面列出的规范目录结构并具有配置 infer_spec_type_from_file_location!,RSpec 会自动包括每种类型的正确支持功能.

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.

添加该配置片段后,我不再需要指定规范类型(例如 type: :controller).

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屋!

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