My pact.io verification is failing and Im not sure why.
I have provided the error message, pact test file, and node.js express server which serves up the data below.
I am getting this error:
0) The following request was expected but not received:
Method: GET
Path: /dogs`enter code here`
Test Code:
const axios = require("axios");
const { Pact } = require("@pact-foundation/pact");
// Implementation of the function to be tested
function getMeDogs() {
return axios
.get("http://localhost:3000/dogs")
.then((response) => {
console.log(response.data);
return response.data;
})
.catch((error) => {
console.error("Error fetching dogs:", error.message);
throw error;
});
}
// Pact contract test
const provider = new Pact({
port: 3000, // Use a free port here
consumer: "Dog Consumer",
provider: "Dog Provider",
});
provider
.setup()
.then(() => {
provider.addInteraction({
uponReceiving: "A request for all dogs",
withRequest: {
method: "GET",
path: "/dogs",
},
willRespondWith: {
status: 200,
body: [
{ name: "Buddy", breed: "Labrador" },
{ name: "Max", breed: "German Shepherd" },
],
},
});
})
.then(() => {
return getMeDogs(); // Call the function under test
})
.then((dogs) => {
console.log(dogs);
})
.then(() => provider.verify())
.finally(() => provider.finalize());
Server:
const express = require("express");
const app = express();
const port = 3000;
// Hardcoded array of dogs
const dogs = [
{ name: "Buddy", breed: "Labrador" },
{ name: "Max", breed: "German Shepherd" },
];
// Route to fetch dogs
app.get("/dogs", (req, res) => {
res.set("Content-Type", "application/json");
res.status(200).json(dogs);
});
// Start the server
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});
Please give me some help! Thanks. I have looked all over for an answer to just a simple pact contract test. I finally found this one to work, mostly, but with the above stated error.