Rails,Devise,Rspec:未定义的方法'sign_in'

ste*_*eel 30 rspec devise ruby-on-rails-4

我正在尝试在Rails中编写Rspec测试,使用Devise帮助方法进行登录和注销.sign_in方法无效.但是,在对应用程序进行一系列更改之前,它已经提前工作了.

我尝试过的事情:

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

到目前为止,没有骰子.使用已登录用户测试控制器需要做些什么?

错误信息:

 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)>'
Run Code Online (Sandbox Code Playgroud)

控制器规格

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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

的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
Run Code Online (Sandbox Code Playgroud)

工厂/ 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
Run Code Online (Sandbox Code Playgroud)

提前致谢.

Tyl*_*ier 29

您最近是否像我一样升级到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
Run Code Online (Sandbox Code Playgroud)

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

如果您按照上面列出的规范目录结构并配置了infer_spec_type_from_file_location !,则RSpec将自动为每种类型包含正确的支持函数.

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


dan*_*i24 9

如果您需要请求规范sign_in文件中的方法,那么您应该包含以下内容:

config.include Devise::Test::IntegrationHelpers, type: :request
Run Code Online (Sandbox Code Playgroud)


ste*_*eel 8

我想出了一个解决方案.我明确地将控制器的描述块定义为控制器类型.

describe OrderItemsController, :type => :controller do
Run Code Online (Sandbox Code Playgroud)

我仍然不明白为什么这段代码早先工作但现在需要这个(看似多余的)显式声明.无论如何,我很感激学习这里发生的事情.谢谢!


aar*_*rio 7

您可以使用文件 spec/spec_helper.rb 中的设计助手:

RSpec.configure do |config|
   config.include Devise::TestHelpers, type: :controller
end
Run Code Online (Sandbox Code Playgroud)


Jan*_*dek 6

我可以为你提供一个例子(适用于我 - rspec/capybara/simplecov等...)

投机/ spec_helper.rb

 require 'capybara/rspec'
 require 'capybara/rails'

 RSpec.configure do |config|
  config.use_transactional_fixtures = true

  config.infer_base_class_for_anonymous_controllers = false

  config.include FactoryGirl::Syntax::Methods
  config.include Devise::TestHelpers, type: :controller
  config.include Capybara::DSL
  config.include Warden::Test::Helpers
  config.include Rails.application.routes.url_helpers
end
Run Code Online (Sandbox Code Playgroud)

规格/集成/ user_flow_spec.rb

require 'spec_helper'

feature 'Verify contract' do
  # Create employee
  let(:employee) { create(:employee) }
  let (:book) { create(:book) }

  # Sign in employee before each test!
  before :each do
    login_as employee, scope: :user
  end

  scenario 'create book' do
    # Visit Index and click to create
    visit employee_books_path
    click_link 'Create'
    expect(current_path).to eq(employee_books_path)
  end
end
Run Code Online (Sandbox Code Playgroud)

我希望它会好的:)我认为你的问题是缺少Warden测试助手......