【问题标题】:Devise sign_in for api causing RSpec test to fail为 api 设计 sign_in 导致 RSpec 测试失败
【发布时间】:2014-02-07 13:49:42
【问题描述】:

我正在使用 Rails 和 Devise 构建 API。我的会话控制器继承自以下基本控制器

api/base_controller.rb 

module Api
  class BaseController < ApplicationController
    skip_before_filter  :verify_authenticity_token
    before_filter :authenticate_user_from_token!
    respond_to :json

    private

    def authenticate_user_from_token!
        user_token = params[:auth_token].presence
        user       = user_token && User.find_by_authentication_token(user_token)

        if user
          sign_in user, store: false
        else
          render :json => {:success => false, :message => "Error with your credentials",     :status => 401}
        end
    end
  end
end

我的会话控制器的销毁操作如下:

api/sessions_controller.rb

before_filter :authenticate_user_from_token!, :except => [:create]


def destroy
  current_user.reset_authentication_token
  render :json => {
    :success => true,
    :status => 200
  }
end

这在通过 curl 测试 api 时非常有效。但是,我无法让我的 Rspec 测试通过破坏操作。来自 Rspec 的 sign_in 用户调用失败,因此响应是重定向。我尝试存根 sign_in 方法没有任何成功。

Rspec 测试:

describe "DELETE destroy" do
  before(:each) do
    @user1 = User.create!(:email => 'example@gmail.com', :password => 'helloworld', :password_confirmation => 'helloworld')
  end

  it "should render success json" do
    delete :destroy, :auth_token => @user1.authentication_token
    json = JSON.parse(response.body)
    json.should include('success' => true, 'status' => 200)
  end

  ###this fails because the response is a redirect to the sign_in page
end

我应该如何模拟从基本控制器中调用的 sign_in 方法?

【问题讨论】:

    标签: ruby-on-rails rspec devise


    【解决方案1】:

    添加包含此内容的spec/support/devise.rb 文件:

    RSpec.configure do |config|
      config.include Devise::TestHelpers, :type => :controller
    end
    

    另外,检查您的 test.log 是否实际使用 json 格式。我遇到了类似的问题,发现我必须在我的规范调用参数中强制使用format :json

    【讨论】:

    • 我已将其包含在我的 spec_helper.rb 文件中。我可以在 Rspec 测试中使用 sign_in(@user) 就好了,但是来自基本控制器内的 sign_in 调用仍然导致重定向到 sign_in 路径。
    • 你能检查一下你的 test.log 是否真的使用 json 格式?我遇到了类似的问题,发现我必须在我的规范调用中强制使用 format :json
    • 感谢您指点我的测试日志!它请求 HTML,但这似乎不是问题。我意识到我正在使用可确认的设计,并且正在创建一个未经确认的用户,导致登录时重定向。我设置了@user1.skip_confirmation!并且所有测试都通过了!
    • 这修复了我从我的 rspec 测试中得到的一个错误... NoMethodError: undefined method `user' for nil:NilClass 立即谷歌没有发现任何东西,希望这个评论有帮助。
    【解决方案2】:

    Andreamazz 将我指向 test.logs,其中显示我创建的用户已被确认(我正在使用可确认的 Devise)。我使用 user.confirm!在 before(:each) 中,一切都在过去。

    describe "DELETE destroy" do
      before(:each) do
        @user1 = User.create!(:email => 'example@gmail.com', :password => 'helloworld',   :password_confirmation => 'helloworld')
        @user1.confirm!
      end
    
      it "should render success json" do
        delete :destroy, :auth_token => @user1.authentication_token
        json = JSON.parse(response.body)
        json.should include('success' => true, 'status' => 200)
      end
    end
    

    谢谢!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-08
      • 2017-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多