1

The response body is return the "formated json with Grape Entity", but the first_prefered is returning the complete object (json format).

How Can I convert the first_prefered object just to get the exposed fields using grape entity?

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

Test:

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
Ivan Santos
  • 626
  • 2
  • 7
  • 19

2 Answers2

3

The Grape::Entity class has an represent method. So

API::V1::Entities::FeaturedHomekeeperResponseEntity.represent first_prefered

will return you the presenter object.

 API::V1::Entities::FeaturedHomekeeperResponseEntity.represent(first_prefered).to_json()

Should return the json you want.

slowjack2k
  • 2,566
  • 1
  • 15
  • 23
3

I don't think you should use Grape::Entity to format data inside your test scenario. Since this is an acceptance/integration test, which is supposed to be written from the user perspective. it should contain the least amount possible of code related things. IMHO you should pick the keys/values out of the JSON manually.