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?
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:
look forward to the reply.