rails rspec stubbed ActiveRecord::Relation for ActiveRecord Serialize

Viewed 443

can I stub ActiveRecord::Relation for ActiveRecord Serialize !?

I could stub ActiveRecord::Relation .. but it doesn't work for ActiveRecord Serialize !

In my controller, I have sth like this:

@res = ModelName.includes(...).where(...).order(..).page(..).per(..)
render json: {
   res: ActiveModelSerializers::SerializableResource.new(
      res,
      each_serializer: ModelNameSerializer,
      meta: pagination_dict(@res)
      adapter: :json
   ).as_json
 }

In rspec test file, I did sth like this:

relation = ModelName.all.page(1)
allow(relation).to receive(:[]).and_return([obj_1, obj_2])
allow(ModelName).to receive_message_chain(:includes, :where, :order, :page, :per).and_return(
      relation
    )

there's no error, but still Serialize return 0 object!, so stub operator [] doesn't work for each_serializer !

any idea ?!, thanks

0 Answers
Related