Generate multiple requests within one pact file

645 views
Skip to first unread message

Levi Lee

unread,
Jul 29, 2016, 2:46:32 AM7/29/16
to Pact
Hi, guys,

Hope you are doing great. 
This week, I have figured a couple of issues for pact, now I think am getting close to get all my pact done.
And, here, Ive got another question, with my pact.spec.js, when I specify one scenario, let's say, one Get, then this dsl can help me generate one pact file, which is awesome.
But when I have multiple calls, then the generated pact file will only cover the very last Scenario, lets see Ive specified tests for Get/Post, but only Post is generated into the final pact file.
So, can I ask if there is a way where I can bundle multiple requests, make Get/POST/DELETE in one single json file?

here Ive got one instance:

this is my test code:

var Pact       = require('pact')
var wrapper    = require('@pact-foundation/pact-node')
var path       = require('path')
var chai   = require("chai");
var chaiAsPromised = require("chai-as-promised");
var request    = require('superagent')
var Promise    = require('bluebird')

chai.use(chaiAsPromised);

describe('Simple Pact Test', () => {

const MOCK_PORT = 9999
  const PROVIDER_URL = `http://localhost:${MOCK_PORT}`
  const mockServer = wrapper.createServer({
    port: MOCK_PORT,
    log: path.resolve(process.cwd(), 'test/pact', 'pact.log'),
    dir: path.resolve(process.cwd(), 'test/pact'),
    spec: 1
  })

  const EXPECTED_BODY = [{
    savedSearches: [
      {id: 1, name: 'test in mel'},
      {id: 2, name: 'driver in syd'},
      {id: 3, name: 'manager in bri'},
      {id: 4, name: 'miner in per'}
    ]
  }]

  var pact

  after(() => {
    wrapper.removeAllServers()
  })

  beforeEach((done) => {
    mockServer.start().then(() => {
      pact = Pact({ consumer: `Saved Search UI`, provider: `Saved Search Exp API`, port: MOCK_PORT })
      done()
    })
  })

  afterEach((done) => {
    mockServer.delete().then(() => {
      done()
    })
  })

  describe('with a single Get request', () => {

    // add interactions, as many as needed
    beforeEach((done) => {
      pact.addInteraction({
        state: 'i have a list of saved searches',
        uponReceiving: 'a request for a saved search',
        withRequest: {
          method: 'get',
          path: '/savedSearch',
          headers: { 'Accept': 'application/json' }
        },
        willRespondWith: {
          status: 200,
          headers: { 'Content-Type': 'application/json' },
          body: EXPECTED_BODY
        }
      }).then(() => done())
    })

    // once test is run, write pact and remove interactions
    afterEach((done) => {
      pact.finalize().then(() => done())
    })

    // execute assertions
    it('successfully verifies', (done) => {
      const verificationPromise = request
        .get(`${PROVIDER_URL}/savedSearch`)
        .set({ 'Accept': 'application/json' })
        .then(pact.verify)

      chai.expect(verificationPromise).to.eventually.equal(JSON.stringify(EXPECTED_BODY)).notify(done)
    })
  })

  describe('Experiment Post request', () => {

    beforeEach((done) => {
      pact.addInteraction({
        state: 'i have a list of projects',
        uponReceiving: 'request to post a savedSearch',
        withRequest: {
          method: 'post',
          path: '/savedSearch',
          headers: { 'Accept': 'application/json' },
          body: {
            'note' : 'driver in Adl'
          }
        },
        willRespondWith: {
          status: 200
        }
      }).then(() => done())
    })


    afterEach((done) => {
      pact.finalize().then(() => done())
    })


    it('successfully verifies', (done) => {
      const verificationPromise = request
        .post(`${PROVIDER_URL}/savedSearch`)
        .send({ note: 'driver in Adl' })
        .set({ 'Accept': 'application/json' })
        .then(pact.verify)

      chai.expect(verificationPromise).to.eventually.equal('').notify(done)
    })
  })
});

by running this, my generated pact file will only include Post, as opposed to both, and here is my generated pact file:

{
  "consumer": {
    "name": "Saved Search UI"
  },
  "provider": {
    "name": "Saved Search Exp API"
  },
  "interactions": [
    {
      "description": "request to post a savedSearch",
      "provider_state": "i have a list of projects",
      "request": {
        "method": "POST",
        "path": "/savedSearch",
        "headers": {
          "Accept": "application/json"
        },
        "body": {
          "note": "driver in Adl"
        }
      },
      "response": {
        "status": 200,
        "headers": {
        }
      }
    }
  ],
  "metadata": {
    "pactSpecificationVersion": "1.0.0"
  }
}


so, can u please do me a favour please?

look forward to the reply.

Matt Fellows

unread,
Jul 30, 2016, 6:05:49 AM7/30/16
to Levi Lee, Pact

Hi Levi, you should only call finalize() once across a single pact file.

In your case, I'd remove it from the afterEach() block and into the after() block.


--
You received this message because you are subscribed to the Google Groups "Pact" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pact-support...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Matt Fellows

unread,
Jul 30, 2016, 7:34:44 AM7/30/16
to Levi Lee, Pact
Reply all
Reply to author
Forward
0 new messages