Rspec.NoMethodError:#的未定义方法`sign_in'

时间:2014-12-25 15:39:57

标签: ruby-on-rails rspec

我正在尝试在我的控制器中测试管理员用户,但是当我运行测试时,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

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

您是否包含了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

答案 1 :(得分:0)

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

include Devise::Test::IntegrationHelpers