未定义的方法' sign_in'对于rspec

时间:2016-06-07 14:30:40

标签: ruby-on-rails ruby-on-rails-4 rspec devise

你好,所以我一直收到这个错误,虽然我尝试了一些在这里发布的解决方案

规格/支持/ devise.rb

RSpec.configure do |config|
 config.include Devise::Test::ControllerHelpers, type: :controller
 config.include Devise::Test::ControllerHelpers, type: :view
end

accounts_spec.rb

require "rails_helper"

RSpec.describe Api::V1::AccountsController, :type => :controller do
  describe "GET index no account" do
    it "has a 403 status code" do
      get :index
      expect(response.status).to eq(403)
    end
  end
  describe "GET index with account" do
    it "has a 200 status code" do
      sign_in @user
      get :index
      expect(response.status).to eq(200)
    end
  end
end

accounts_controller.rb

class Api::V1::AccountsController < ApplicationController
    skip_before_action :verify_authenticity_token

    def index
        #show user details
        raise if not current_user
        render json: { :user => current_user.as_json(:except=>[:created_at, :updated_at, :authorization_token, :provider, :uid, :id])}
        rescue
        render nothing: true, status: 403   
    end

有什么想法吗?我被阻止了,403的第一次测试工作,但后来我就不能使用设计的sign_in了......

3 个答案:

答案 0 :(得分:0)

看起来配置没有运行。 确保您的spec/support/devise.rb包含在

之类的内容中
Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
rails_helper.rb

中的

或将这些config.include移至rails_helper本身

答案 1 :(得分:0)

问题是devise.rb文件。正确的配置行是

  config.include Devise::TestHelpers, type: :controller
  config.include Devise::TestHelpers, type: :view

答案 2 :(得分:0)

您可以直接在rails helper

中使用它
config.include Devise::TestHelpers, :type => :controller

使用sign_in方法。