如何解决试图用 Mocha 和 Sinon 存根 HTTP 调用收到错误:TypeError:预期会产生,但没有传递回调
我一直在练习使用 JSONPlaceholder、Mocha、Chai 和 Sinon 进行存根以掌握这一点,稍后我将单元测试我对 {{ 1}} 次调用。
我使用 Axios 创建了一个 HTTP
来获取所有 function
并导出为这样的存根:
todos
然后我试着像这样存根:
const axios = require('axios');
const getAllTodos = () => {
return axios.get('https://jsonplaceholder.typicode.com/todos').then(response => response);
};
module.exports = { getAllTodos };
但是,我收到以下错误:
const { expect } = require('chai');
const sinon = require('sinon');
const axios = require('axios');
const { getAllTodos } = require('correct/path/to/method');
describe('Testing the todos HTTP call',() => {
before(() => {
sinon.stub(axios,'get').yields(null,null,JSON.stringify([
{
userId: 1,id: 1,title: "delectus aut autem",completed: false
},]))
});
after(() => {
axios.get.restore();
});
it('should return all todos',done => {
getAllTodos().then(todos => {
todos.data.forEach(todo => {
expect(todo).to.have.property('userId');
expect(todo).to.have.property('id');
expect(todo).to.have.property('title');
expect(todo).to.have.property('completed');
});
done();
}).catch(done,done);
});
});
如果我对 1) Testing the todos HTTP call
should return all todos:
TypeError: get expected to yield,but no callback was passed. Received [https://jsonplaceholder.typicode.com/todos]
进行单元测试而不对其进行存根。它就像一个魅力。这是没有被存根的单元测试:
function
我使用正确的断言正确传递了 const { expect } = require('chai');
const { getAllTodos } = require('correct/path/to/method');
describe('Testing the todos HTTP call',() => {
it('should return all todos',done);
});
});
:
function
我该如何解决这个问题?我做错了什么?我需要一个适合初学者的解释,才能将此解决方案转化为我自己的实现。
编辑:
我一直在尝试其他方法来存根
Testing the todos HTTP call
✓ should return all todos (169ms)
调用,它适用于互联网,但是一旦我关闭了互联网……HTTP
就会失败。这是我最新的代码:
stub
&导出的describe('Testing the todos HTTP call',() => {
let stub;
beforeEach(() => {
stub = sinon.stub({ getAllTodos },'getAllTodos');
});
afterEach(() => {
stub.restore();
});
it('should return all todos with the right properties',() => {
const mockedResponSEObj = {
userId: 1,title: 'This is title',completed: true,};
stub
.withArgs('https://jsonplaceholder.typicode.com/todos')
.returns(Promise.resolve(mockedResponSEObj));
const result = getAllTodos('https://jsonplaceholder.typicode.com/todos');
expect(result.data[0]).to.have.property('userId');
expect(result.data[0]).to.have.property('id');
expect(result.data[0]).to.have.property('title');
expect(result.data[0]).to.have.property('completed');
});
});
最新代码:
function
解决方法
我能够通过存根 getALLTodos
对 Axios
方法进行单元测试,而是使用 GET
方法,如下所示:
describe('Testing the todos HTTP call',() => {
let stub;
const mockedResponseObj = {
userId: 1,id: 1,title: 'This is title',completed: true,};
beforeEach(() => {
stub = sinon.stub(axios,'get').resolves(mockedResponseObj);
});
afterEach(() => {
stub.restore();
});
it('should return all todos with the right properties',done => {
getAllTodos('https://jsonplaceholder.typicode.com/todos').then(res => {
expect(res.data[0]).to.have.keys(Object.keys(mockedResponseObj));
});
done();
});
});
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。