我正在尝试为我的rails api应用程序编写控制器规范。使用设计和一些自定义身份验证令牌方法完成身份验证。这是auth方法:
def authenticate_user_with_token!
token = params[:auth_token]
if user = User.find_by_authentication_token(token)
puts user
sign_in user, store: false
else
fail InvalidAuthTokenError
end
end
但是,运行以下规范:
require 'rails_helper'
RSpec.describe Api::V1::JobsController, :type => :controller, current: true do
describe "GET #index" do
before do
@advisor = FactoryGirl.create(:user, :service_advisor)
end
it "responds successfully with an HTTP 200 status code" do
get :index, auth_token: @advisor.authentication_token
expect(response.status).to eq(200)
end
end
end
抛出:
NoMethodError:
undefined method `user' for nil:NilClass
就在那一行:sign_in user, store: false
。你知道我做错了什么吗?
Rails 4.1.0
Ruby 2
RSpec 3