javascript 使用 Moxios 模拟 Axios 调用以测试 API 调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46179297/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mocking Axios calls using Moxios in order to test API calls
提问by César Alberca
I have the following custom Axios instance:
我有以下自定义 Axios 实例:
import axios from 'axios'
export const BASE_URL = 'http://jsonplaceholder.typicode.com'
export default axios.create({
baseURL: BASE_URL
})
With the corresponding service:
配合相应的服务:
import http from './http'
export async function fetchUserPosts(id) {
const reponse = await http.get(`/users/${id}/posts`)
return reponse.data
}
And this is the test for said service:
这是对上述服务的测试:
import moxios from 'moxios'
import sinon from 'sinon'
import http from '@/api/http'
import { fetchUserPosts } from '@/api/usersService'
describe('users service', () => {
beforeEach(() => {
moxios.install(http)
})
afterEach(() => {
moxios.uninstall(http)
})
it('fetches the posts of a given user', (done) => {
const id = 1
const expectedPosts = ['Post1', 'Post2']
moxios.stubRequest(`/users/${id}/posts`, {
status: 200,
response: expectedPosts
})
const onFulfilled = sinon.spy()
fetchUserPosts(1).then(onFulfilled)
moxios.wait(() => {
expect(onFulfilled.getCall(0).args[0].data).toBe(expectedPosts)
done()
})
})
})
Which when executed using Karma + Jasmine raises the following error:
使用 Karma + Jasmine 执行时会引发以下错误:
Uncaught TypeError: Cannot read property 'args' of null thrown
What I would like to test is that when the endpoint /users/{id}/postsis hit a mocked response is sent back. All this while using my custom axios instance http.
我想测试的是,当端点/users/{id}/posts被击中时,会发回模拟响应。所有这些都是在使用我的自定义 axios 实例时http。
I've tried stubbing as the first example of the documentation of moxios shows. However I don't think that fits my use case, as I would like to check that the request is formed correctly in my service.
我已经尝试将 stubbing 作为 moxios 文档的第一个示例显示。但是我认为这不适合我的用例,因为我想检查请求是否在我的服务中正确形成。
I've also tried with the following code, which works as expected, however I would like to test my service (which the following code does not do):
我还尝试了以下代码,它按预期工作,但是我想测试我的服务(以下代码不这样做):
import axios from 'axios'
import moxios from 'moxios'
import sinon from 'sinon'
describe('users service', () => {
beforeEach(() => {
moxios.install()
})
afterEach(() => {
moxios.uninstall()
})
it('fetches the posts of a given user', (done) => {
const id = 1
const expectedPosts = ['Post1', 'Post2']
moxios.stubRequest(`/users/${id}/posts`, {
status: 200,
response: expectedPosts
})
const onFulfilled = sinon.spy()
axios.get(`/users/${id}/posts`).then(onFulfilled)
moxios.wait(() => {
expect(onFulfilled.getCall(0).args[0].data).toBe(expectedPosts)
done()
})
})
})
Any ideas on how could I fix the error?
关于如何修复错误的任何想法?
回答by Elmer Dantas
I know that is an old question but as I came across with it with the same problem, I'll post my approach:
我知道这是一个老问题,但当我遇到同样的问题时,我将发布我的方法:
You don't need to use sinonin this case, as you have an instance of axios, use it to configure moxios(as you already have done)
sinon在这种情况下您不需要使用,因为您有一个 实例axios,使用它来配置moxios(正如您已经完成的那样)
beforeEach(() => {
moxios.install(http)
})
afterEach(() => {
moxios.uninstall(http)
})
then you test your method like this:
然后你像这样测试你的方法:
it('test get', async () => {
const expectedPosts = ['Post1', 'Post2']
moxios.wait(() => {
const request = moxios.requests.mostRecent()
request.respondWith({ status: 200, response: expectedPosts }) //mocked response
})
const result = await fetchUserPosts(1)
console.log(result) // ['Post1','Post2']
expect(result).toEqual(expectedPosts)
})
That's it.
而已。
Regards
问候

