任务 1:练习 API 契约测试
什么是契约测试?

练习目标
任务分解(API Tasking)
编写契约测试(Contract Testing)
启动 Mock Server
本次任务的练习要求
也请你思考一下
契约测试与其他测试的关系?



在 E2E 层面如何使用 Mock Server?

从协同的角度来说,GraphQL 的价值是什么?


阅读材料
最后更新于
git clone https://github.com/JimmyLv/tdd-bookshelf.git
cd tdd-bookshelf
yarn install #安装依赖
yarn start #启动应用yarn pact:test #运行 pact 测试test('getting all books', async () => {
// Given: set up Pact interactions
await provider.addInteraction({
state: 'books exist',
uponReceiving: 'get all books',
withRequest: {
method: 'GET',
path: '/api/books',
},
willRespondWith: {
status: 200,
headers: {
'Content-Type': 'application/json; charset=utf-8',
},
body: like([somethingLike(bookContent), somethingLike(anotherBookContent)]),
},
})
// When: make request to Pact mock server
const response = await API.getAllBooks()
// Then: verify the response data and status
expect(response.data).toStrictEqual([bookContent, anotherBookContent])
expect(response.status).toEqual(200)
})yarn pact:stubs #启动 Pact Stub serverconst pact = require('@pact-foundation/pact-node')
const path = require('path')
const PACT_FILE = path.resolve(__dirname, '../tests/pacts/contracts/bookshelf-bookapi.json')
const server = pact.createStub({
pactUrls: [PACT_FILE],
port: 8080,
cors: true,
})
server.start().then(function () {
console.info(`Mock server running on http://localhost:${8080}/api`)
// Do your testing/development here
})cy.server()
cy.fixture('books.json').as('booksJSON')
cy.route('GET', 'books/*', '@booksJSON')