Rspec/Mongoid testing JSON response fails, Expecting field names, but getting fiel Alias instead

I am trying to test my controller for JSON response,

Here is my Model :

class SalesRep < Person
 include Mongoid::Document

#Here is Class Person which SalesRep Inherits from:

class Person
include Mongoid::Document

field :nf,  as: :first_name,    type: String
field :nl,  as: :last_name,     type: String
field :ttl, as: :title,         type: String
field :ph,  as: :phone_num,     type: String
field :em,  as: :email,         type: String

attr_accessible :first_name,
                :phone_num ,


Here is my Controller#index :

def index
@sales_reps = SalesRep.all

respond_to do |format|
  format.html # index.html.erb
  format.json { render json: @sales_reps, root: false }

And here is my rspec test that i am trying to test JSON response with :

  require 'spec_helper'
  describe SalesRepsController do
  let(:valid_attributes) { { first_name: "Seth", last_name: "McFee" } }
  describe "GET index" do
    let!(:sales_rep1){ SalesRep.create! valid_attributes}
    it "return JSON-formated content" do
      get :index, format: :json
      expect(response.body).to have_content sales_rep1.to_json

and, here is the Serializer :

class SalesRepSerializer < ActiveModel::Serializer
  attributes :id, :first_name, :last_name

and here is the test outpout : 1) SalesRepsController GET index return JSON-formated content

Failure/Error: expect(response.body).to have_content sales_rep1.to_json
expected to find text "{\"_id\":\"52171a4fd7037ee84e000001\",\"em\":null,\"meeting_ids\":[],\"nf\":\"Seth\",\"nl\":\"McFee\",\"ph\":null,\"sex\":1,\"ttl\":null}" in "[{\"id\":\"521718ced7037e15c2000001\",\"first_name\":\"Mark\",\"last_name\":\"Doe\"},{\"id\":\"52171a4fd7037ee84e000001\",\"first_name\":\"Seth\",\"last_name\":\"McFee\"}]"

As you can see, the test is expecting to find nf, and nl , but instead it's receiving the fields Aliases (first_name, last_name respectively ), I see the problem , but i don't know How to fix it !

Any help/Advice/Tip/keyword to use while searching. is highly appreciated.

PS, some of the data is deprecated to make the post more readable,

PPS, sorry for my bad English .


Right now, on all mongoid versions, thats the behaviour. JSON serialization dont use the alias fields on encode. There is a PR to fix this which is on debate still. If you want that feature, you can merge the PR into your own fork. Also you can vote on the ticket in order to get merged sooner.

Need Your Help

Getting Error while creating spinner programmatically


I have a button in my xml. I want to pop up spinner programmatically not from xml after

Android Vibration APIs

android android-audiomanager vibration

I am experimenting with sound manager application to create different sound profiles for "RING, NOTIFICATION, MEDIA, SYSTEM".

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.