rails rspec stubbed ActiveRecord :: ActiveRecord Serialize的关系

时间:2017-11-07 10:22:27

标签: ruby-on-rails activerecord rspec rails-activerecord rspec-rails

我可以将ActiveRecord :: Relation存储为ActiveRecord Serialize!?

我可以存根ActiveRecord :: Relation ..但它不能用于ActiveRecord Serialize!

在我的控制器中,我有这样的感觉:

@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
 }

在rspec测试文件中,我确实是这样的:

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
    )

没有错误,但仍然是Serialize返回0对象!,因此存根运算符[]不适用于each_serializer

任何想法?!,谢谢

0 个答案:

没有答案