是否可以使用moxios模拟对POST请求的回复,该请求不仅可以通过URL匹配,还可以通过POST正文匹配?事后检查身体对我也有用.
这就是我现在正在做的事情.据我所知,没有方法特定的存根方法:
describe('createCode', function () {
it('should create new code', function () {
moxios.stubRequest(process.env.API_URL + '/games/GM01/codes', {
status: 200
})
})
})
Run Code Online (Sandbox Code Playgroud) 我尝试在从服务器获取某些内容后测试其渲染。我使用Vue Test Utils
但这无关紧要。
在created
组件的钩子中,ajax 调用是通过axios
. 我注册axios-mock-adapter
响应并“渲染”组件,进行调用,一切正常,但我必须使用该moxios
库来等待请求完成。
it('displays metrics', (done) => {
this.mock.onGet('/pl/metrics').reply((config) => {
let value = 0
if (config.params.start == '2020-01-26') {
value = 80
}
if (config.params.start == '2020-01-28') {
value = 100
}
return [200, {
metrics: [
{
key: "i18n-key",
type: "count",
value: value
}
]
}]
})
.onAny().reply(404)
let wrapper = mount(Dashboard)
moxios.wait(function() {
let text = wrapper.text()
expect(text).toContain('80')
expect(text).toContain('100')
expect(text).toContain('+20')
done()
})
})
Run Code Online (Sandbox Code Playgroud)
是否有可能摆脱 …
我正在尝试为 VueJS 组件编写我的第一个单元测试,该组件在呈现时从 API 端点获取一些数据:
我的 VueJS 组件:
import axios from 'axios';
export default {
props: {
userIndexUrl: {
required: true
}
},
data() {
userList: [],
tableIsLoading: true
},
created() {
const url = this.userIndexUrl;
axios.get(url)
.then(response => {
this.userList = response.data.data;
this.tableIsLoading = false;
})
},
}
Run Code Online (Sandbox Code Playgroud)
和我的测试:
import { mount } from 'vue-test-utils'
import moxios from 'moxios'
import UsersAddRemove from 'users_add_remove.vue'
describe('UsersAddRemove', () => {
const PROPS_DATA = {
userIndexUrl: '/users.json'
}
beforeEach(() => {
moxios.install();
moxios.stubRequest('/users1.json', …
Run Code Online (Sandbox Code Playgroud) 尝试测试axios
调用并尝试moxios
包。
"axios": "^0.16.2",
"moxios": "^0.4.0",
在这里找到:https : //github.com/axios/moxios
按照那里的例子,但我的测试错误就moxios.install()
行了:
import axios from 'axios'
import moxios from 'moxios'
import sinon from 'sinon'
import { equal } from 'assert'
describe('mocking axios requests', function () {
describe('across entire suite', function () {
beforeEach(function () {
// import and pass your custom axios instance to this method
moxios.install()
})
Run Code Online (Sandbox Code Playgroud)
import axios from 'axios';
import moxios from 'moxios';
import sinon from 'sinon';
import { equal } from …
Run Code Online (Sandbox Code Playgroud) 我有以下自定义Axios实例:
import axios from 'axios'
export const BASE_URL = 'http://jsonplaceholder.typicode.com'
export default axios.create({
baseURL: BASE_URL
})
Run Code Online (Sandbox Code Playgroud)
配合相应的服务:
import http from './http'
export async function fetchUserPosts(id) {
const reponse = await http.get(`/users/${id}/posts`)
return reponse.data
}
Run Code Online (Sandbox Code Playgroud)
这是对上述服务的测试:
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 …
Run Code Online (Sandbox Code Playgroud) 有一个页面在渲染时发出多个HTTP获取数据请求。如何过滤moxios.requests
以获取具有特定URL的请求以进行响应?
我可以在网上找到的唯一方法是moxios.requests.mostRecent()
,但是我不确定最近的请求是什么。即使这样,还有另一种方法可以弹出另一个请求吗?
此处的moxios库上似乎没有任何文档:https : //github.com/axios/moxios
let request = moxios.requests.mostRecent() // How to get 'some/url/' here? This expect is currently failing
expect(request.url).to.equal('some/url/')
request.respondWith({
status: 200,
response: []
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用钩子测试反应功能组件。useEffect 钩子调用第三方 API,然后在返回时调用 setState。
我有测试工作,但不断收到警告,指出组件的更新未包含在行为中。
我遇到的问题是期望在 moxios.wait 承诺内,因此我无法将其包装在行为函数中,然后对其结果进行断言。
测试通过了,但我知道不包装在行为函数中更新状态的代码可能会导致误报或未发现的错误。我只是想知道我应该如何测试这个。
我已经尝试在 react 16.9.0 alpha 版本中使用新的 async await act 函数,以及我在许多 github 问题(如 jest setTimers)中发现的许多建议,但似乎都没有解决问题。
组件
const Benefits = props => {
const [benefits, setBenefits] = useState([])
const [editing, setEditing] = useState(false)
const [editingBenefit, setEditingBenefit] = useState({id: null, name: '', category: ''})
useEffect(() => {
axios.get('#someurl')
.then(response => {
setBenefits(response.data)
})
}, [])
}
Run Code Online (Sandbox Code Playgroud)
考试
describe('Benefits', () => {
it('fetches the list of benefits from an api and populates the benefits table', (done) …
Run Code Online (Sandbox Code Playgroud)