Reputation: 2923
I was struggling with undefined method 'authenticate' for nil:NilClass
for my requests using the serializer (Rspec).
api/users_controller_spec.rb
require 'rails_helper'
describe Api::V1::UsersController, type: :controller do
describe "GET #show" do
context "with valid credentials" do
let!(:application) { create(:doorkeeper_application) } # OAuth application
let!(:user) { create(:user) }
let!(:token) { create(:doorkeeper_access_token, application_id: application.id, resource_owner_id: user.id) }
before do
allow(controller).to receive(:doorkeeper_token) {token}
end
context 'and valid request' do
before(:each) do
get :show, format: :json
@json = JSON.parse(response.body)
end
it "returns the user with 'id'" do
expect(@json["id"]).to_not be_nil
end
end
end
end
end
serializers/user_serializer.rb
class UserSerializer < ActiveModel::Serializer
attributes :id, :uid, :name, :email
end
Upvotes: 2
Views: 1038
Reputation: 2923
Turns out all I needed was to add serialization_scope :view_context
to my ApplicationController
but I only discovered it when watching the amazing Railcast about the gem: http://railscasts.com/episodes/409-active-model-serializers?view=asciicast
Don't use active_model_serializer > 0.9.x otherwise it will break in production. https://github.com/rails-api/active_model_serializers/commit/0d31e72d2211b6bf7f0b0420139c4b370d6e986e https://github.com/rails-api/active_model_serializers/issues/139
Upvotes: 1