rspecgrape-apigrape-entity

How can I do request tests with Grape Entity?


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

Solution

  • 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.