Testing the same API for multiple answer sets

We tried to test an API impacted by a microservice (like GET / contacts) that is being consumed by another microservice.

To avoid integration tests, we developed consumer based contract tests where the consumer microservice created the pacts and published them to the broker, from where the manufacturer verified the contract separately.

We've used Pact IO to achieve this and it has been pretty good so far.

Now we run into problems when trying to run exhaustive tests where we want to see an empty list returned from GET / contacts.

The problem is this: when adding interactions, we could use vendor states, but we couldn't find a way to distinguish between writing tests to get a contact list from GET / contacts once and getting an empty list in another test.

This is how we create pact tests in our consumer microservice:

mockServer.start()
        .then(() => {
          provider = pact({
            // config
          })
          return provider.addInteraction({
            state: 'Get all contacts',
            uponReceiving: 'Get all contacts',
            withRequest: {
              method: 'GET',
              path: '/contacts',
              headers: {
                Accept: 'application/json; charset=utf-8'
              }
            },
            willRespondWith: {
              status: 200,
              body: //list of all contacts
            }
          })
        .then(() => {
          return provider.addInteraction({
            state: 'Get empty list of contacts',
            uponReceiving: 'Get empty list of contacts',
            withRequest: {
              method: 'GET',
              path: '/contacts',
              headers: {
                Accept: 'application/json; charset=utf-8'
              }
            },
            willRespondWith: {
              status: 200,
              body: [] // empty list
            }
          })
        })

      

We cannot find a way to distinguish between these interactions in our tests !:(

Any help would be appreciated!

Thank.

+3


source to share


1 answer


Suppose you are using something like Mocha, you have to separate these interactions into separate tests - for example, call addInteraction

in each block describe

that is contextual for the test case you are using (perhaps in before

to make your tests clearer). In the meantime, there is no need to know about it. โ€

Your general structure might look like this (pseudocode):



context("Contacts exist")
  describe("call some API")
    before()
      provider.addInteraction(interactionWithContacts)

    it("Returns a list of contact objects")
      # your test code here
      # Verify - this will also clear interactions so
      # your next test won't conflict 
      provider.verify()

context("No contacts exist")
  describe("call some API")
    before()
      provider.addInteraction(interactionWithNoContacts)

    it("Returns an empty list of contacts")
      # your test code here
      # Verify - this will also clear interactions so
      # your next test won't conflict 
      provider.verify()       

      

+1


source







All Articles