环境:
NodeJS 8.1.2
axios 0.16.2
axios-mock-adapter 1.9.0
使用JSONPlaceholder的测试 POST API 调用如下:
const expect = require('chai').expect
const MockAdapter = require('axios-mock-adapter')
// Bootstrapping
const PlaceholderApp = {
createComment: function (author, email, message) {
const options = {
method: 'post',
url: 'https://jsonplaceholder.typicode.com/comments',
data: {
name: author,
email: email,
body: message,
}
}
return axios(options)
}
}
// Mock Adapter
const mockHttpClient = new MockAdapter(axios, { delayResponse: 50 })
// mockHttpClient.onPost(/(\/comments)/i, { name: 'author A', email: 'authorA@test.com', body: 'test comment' }).reply(526) // WORKS!
mockHttpClient.onPost(/(\/comments)/i, { email: 'authorA@test.com' }).reply(527) //This won't work. Would like to have something like this to work tho...
mockHttpClient.onAny().passThrough()
// Test cases
describe('PlaceholderApp.createComment', () => {
it("should fail due to mock...", (resolve) => {
PlaceholderApp.createComment('author A', 'authorA@test.com', 'test comment')
.then((res) => {
resolve()
})
.catch((err) => {
resolve(err)
})
})
})
我想知道是否有办法匹配部分POST
数据?