Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial draft for adding pact tests. Failing with error before undefined
- Loading branch information
Francislainy Campos
committed
Oct 16, 2020
1 parent
64c86e6
commit 402c994
Showing
5 changed files
with
99 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
const axios = require('axios'); | ||
const { Product } = require('../model/Product'); | ||
|
||
class ProductApiClient { | ||
constructor(url) { | ||
this.url = url | ||
} | ||
|
||
async getProduct(id) { | ||
return axios.get(`${this.url}/products/${id}`).then(r => new Product(r.data.id, r.data.name, r.data.type)); | ||
} | ||
} | ||
module.exports = { | ||
ProductApiClient | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class Category { | ||
constructor(id, name) { | ||
this.id = id | ||
this.name = name | ||
} | ||
} | ||
module.exports = { | ||
Category | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class Product { | ||
constructor(id, name, type) { | ||
this.id = id | ||
this.name = name | ||
this.type = type | ||
} | ||
} | ||
module.exports = { | ||
Product | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// (1) Import the pact library and matching methods | ||
const { Pact } = require ('@pact-foundation/pact'); | ||
const { ProductApiClient } = require ('../api/api'); | ||
const { Product } = require ('../model/Product'); | ||
const { like, regex } = require ('@pact-foundation/pact/dsl/matchers'); | ||
const chai = require("chai") | ||
const expect = chai.expect | ||
|
||
// (2) Configure our Pact library | ||
const mockProvider = new Pact({ | ||
consumer: 'katacoda-consumer', | ||
provider: 'katacoda-provider', | ||
cors: true // needed for katacoda environment | ||
}); | ||
|
||
|
||
describe('Products API test', () => { | ||
// (3) Setup Pact lifecycle hooks | ||
before(() => mockProvider.setup()); | ||
afterEach(() => mockProvider.verify()); | ||
after(() => mockProvider.finalize()); | ||
|
||
it('get product by ID', async () => { | ||
// (4) Arrange | ||
const expectedProduct = { id: 10, type: 'pizza', name: 'Margharita' } | ||
|
||
await mockProvider.addInteraction({ | ||
state: 'a product with ID 10 exists', | ||
uponReceiving: 'a request to get a product', | ||
withRequest: { | ||
method: 'GET', | ||
path: '/products/10' | ||
}, | ||
willRespondWith: { | ||
status: 200, | ||
headers: { | ||
'Content-Type': regex({generate: 'application/json; charset=utf-8', matcher: '^application\/json'}), | ||
}, | ||
body: like(expectedProduct), | ||
}, | ||
}); | ||
|
||
// (5) Act | ||
const api = new ProductApiClient(mockProvider.mockService.baseUrl); | ||
const product = await api.getProduct(10); | ||
|
||
// (6) Assert that we got the expected response | ||
expect(product).to.deep.equal(new Product(10, 'Margharita', 'pizza')); | ||
}); | ||
}); |