|
define(['client', 'Pact'], function (example, Pact) { |
|
describe("Client", function () { |
|
|
|
var client, provider |
|
|
|
beforeAll(function (done) { |
|
client = example.createClient('http://localhost:1234'); |
|
|
|
console.log(Pact); |
|
// Create a Mock Server test double of your Provider API. If you need multiple Providers for a scenario, you can create as many as these as you need. |
|
provider = Pact({ |
|
consumer: 'Friends Client', |
|
provider: 'Friends API', |
|
spec: '3.0.0' // https://github.com/pact-foundation/pact-specification |
|
}); |
|
|
|
// required for slower Travis CI environment |
|
setTimeout(function () { |
|
console.log('beforeAll done'); |
|
done(); |
|
}, 2000) |
|
|
|
// Required if run with `singleRun: false. In some cases you might want to clear out the expectations of the Mock Service, call this to clear out |
|
// any expectations for the next test run. NOTE: verify() will implicitly call this. |
|
provider.removeInteractions(); |
|
}); |
|
|
|
afterAll(function (done) { |
|
|
|
// Record the interactions registered to the Mock Server into the pact file and shuts it down. |
|
console.log('afterAll provider.finalize'); |
|
provider.finalize().then(function () { |
|
console.log('afterAll provider.finalize done'); |
|
done(); |
|
}, function (err) { |
|
console.log('afterAll provider.finalize error: ' + err); |
|
done.fail(err); |
|
}) |
|
}); |
|
|
|
describe("sayHello", function () { |
|
beforeAll(function (done) { |
|
console.log('addInteraction-->sayHello'); |
|
// Register an expectation on the Mock Server, which must be called by your test case(s). You can add multiple interactions per |
|
// server. These will be validated and written to a pact if successful. |
|
provider.addInteraction({ |
|
uponReceiving: 'a request for hello', |
|
withRequest: { |
|
method: 'GET', |
|
path: '/sayHello' |
|
}, |
|
willRespondWith: { |
|
status: 200, |
|
headers: {"Content-Type": "application/json"}, |
|
body: {reply: "Hello"} |
|
} |
|
}).then(function () { |
|
console.log('addInteraction-->sayHello done'); |
|
done(); |
|
}, function (err) { |
|
console.log('addInteraction-->sayHello error: ' + err); |
|
done.fail(err); |
|
}) |
|
}); |
|
afterAll(function (done) { |
|
console.log('sayHello.afterAll'); |
|
provider.removeInteractions().then(function () { |
|
console.log('sayHello.afterAll done'); |
|
done(); |
|
}); |
|
}); |
|
|
|
it("should say hello", function (done) { |
|
//Run the tests |
|
console.log('client.sayHello'); |
|
client.sayHello().then(function (data) { |
|
console.log('client.sayHello then'); |
|
expect(JSON.parse(data.responseText)).toEqual({reply: "Hello"}); |
|
done(); |
|
}).catch(function (err) { |
|
console.log('client.sayHello error'); |
|
done.fail(err); |
|
}); |
|
}); |
|
|
|
// verify with Pact, and reset expectations |
|
it('successfully verifies', function (done) { |
|
// Checks with the Mock Service if the expected interactions have been exercised. |
|
console.log('provider.verify'); |
|
provider.verify().then(function (a) { |
|
console.log('provider.verify done'); |
|
done(); |
|
}, function (e) { |
|
console.log('provider.verify error: ' + e); |
|
done.fail(e); |
|
}); |
|
}); |
|
}); |
|
}); |
|
}); |