响应正文返回“带有葡萄实体的格式化 json”,但 first_prefered 返回完整对象(json 格式)。
如何转换first_prefered
对象只是为了使用葡萄实体获取暴露的字段?
精选HomekeeperResponseEntity:
module API::V1::Entities
class FeaturedHomekeeperResponseEntity < Grape::Entity
expose :id, documentation: { type: 'integer', desc: 'ID' }
expose :featured_type, documentation: { type: 'string', desc: 'Featured Type' }
end
end
测试:
let(:address) { Fabricate(:address) }
it 'should return the first prefered homekeeper of an address' do
first_prefered = Fabricate(:featured_homekeeper_as_first_prefered, address: address)
get "/api/v1/addresses/#{address.id}/prefered/first"
expect(json).to eq(YAML.load(first_prefered.to_json))
end