【问题标题】:Rspec.NoMethodError: undefined method `sign_in' for #<RSpec:Rspec.NoMethodError:#<RSpec 的未定义方法“sign_in”:
【发布时间】:2014-12-25 15:39:56
【问题描述】:

我正在尝试在我的控制器中测试管理员用户,但是当我运行测试时,shell 返回错误:

  1) Posts GET /edit_post works for edit post
     Failure/Error: sign_in user
     NoMethodError:
       undefined method `sign_in' for #<RSpec:

posts_spec.rb

  describe 'GET /edit_post' do
    it 'works for edit post' do
      user = FactoryGirl.create(:user)
      user.role == 'admin'
      sign_in user
    end
  end

UPD 这是 rails_helper.rb

ENV["RAILS_ENV"] ||= 'test'
require 'spec_helper'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
ActiveRecord::Migration.maintain_test_schema!

RSpec.configure do |config|
  config.include Capybara::DSL
  config.include Devise::TestHelpers, type: :controller
  config.fixture_path = "#{::Rails.root}/spec/fixtures"
  config.use_transactional_fixtures = true
  config.infer_spec_type_from_file_location!
end

我应该如何解决这个问题?

【问题讨论】:

标签: ruby-on-rails rspec


【解决方案1】:

您是否包含了 Devise 的测试助手,以允许您使用 sign_in

# spec/rails_helper.rb or spec/spec_helper.rb or spec/support/devise.rb
RSpec.configure do |config|
  config.include Devise::TestHelpers, type: :controller
end

这里有解释:

http://www.rubydoc.info/github/plataformatec/devise/file/README.md#Test_helpers

【讨论】:

  • 我知道解决方法,但这不起作用。我更新问题
【解决方案2】:

我遇到了这个问题,所以我在test/test_helper.rb 中添加了以下行,它对我有用。

include Devise::Test::IntegrationHelpers

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多