响应体返回“带有葡萄实体的格式化的json”,但是first_prefered返回完整的对象(json格式)。
如何转换first_prefered对象,以便使用葡萄实体获取公开的字段?
FeaturedHomekeeperResponseEntity:
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发布于 2014-10-26 08:09:25
我认为您不应该使用Grape::Entity来格式化测试场景中的数据。因为这是一个验收/集成测试,所以应该从用户的角度来编写。它应该包含尽可能少的代码相关内容。您应该手动从JSON中选择键/值。
发布于 2014-10-26 07:58:20
Entity类有一个表示方法。所以
API::V1::Entities::FeaturedHomekeeperResponseEntity.represent first_prefered将返回演示者对象。
API::V1::Entities::FeaturedHomekeeperResponseEntity.represent(first_prefered).to_json()应该还你想要的json。
https://stackoverflow.com/questions/26569291
复制相似问题