javascript - 用玩笑模拟 axios 和 kitsu
问题描述
我正在创建一些测试,并使用 Kitsu ( https://github.com/wopian/kitsu/tree/master/packages/kitsu ) 作为使用 API 的客户。
我需要模拟 API 请求,但我不能。Kitsu 内部使用 axios,不知道如何模拟结构:
Kitsu {
camel: [Function: index],
resCase: [Function: index$1],
plural: [Function: pluralize] {
plural: [Function],
isPlural: [Function],
singular: [Function],
isSingular: [Function],
addPluralRule: [Function],
addSingularRule: [Function],
addUncountableRule: [Function],
addIrregularRule: [Function]
},
headers: {
Accept: 'application/vnd.api+json',
'Content-Type': 'application/vnd.api+json'
},
axios: [Function: wrap] {
request: [Function: wrap],
getUri: [Function: wrap],
delete: [Function: wrap],
get: [Function: wrap],
我需要在 axios 属性中模拟 get 方法。我怎么能用 jest 做到这一点?谢谢
解决方案
您可以使用axios-mock-adapter
(https://www.npmjs.com/package/axios-mock-adapter)
例如
const axios = require('axios');
const MockAdapter = require('axios-mock-adapter');
const mock = new MockAdapter(axios);
mock.onGet('http://somehost/path').reply(200, 'response goes here');
推荐阅读
- ios - 从 iOS 审核过程中了解崩溃报告
- windows - 用powershell添加本地Windows防火墙规则的最干净的方法是什么
- c# - 删除额外的正文参数或将 WebGetAttribute/WebInvokeAttribute 上的 BodyStyle 属性设置为 Wrapped
- ssl - 谷歌云 - 连接超时,TLS - 握手
- python - 如何按浮动子列表的项目对列表列表进行排序
- php - 如果在特定内容中则跳过匹配
- c# - 打包 URI NUnit 测试问题
- ios - 使用 Cocoapods 将多个 dylib 组合到 iOS 的单个框架中
- php - 如何在 PHP 脚本的 SQL 请求中不使用 IN 运算符过滤搜索结果
- c# - C# XML 反序列化为一张表中的 DataSet