试图用 Mocha 和 Sinon 存根 HTTP 调用收到错误:TypeError:预期会产生,但没有传递回调

如何解决试图用 Mocha 和 Sinon 存根 HTTP 调用收到错误:TypeError:预期会产生,但没有传递回调

我一直在练习使用 JSONPlaceholderMochaChaiSinon 进行存根以掌握这一点,稍后我将单元测试我对 {{ 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

解决方法

我能够通过存根 getALLTodosAxios 方法进行单元测试,而是使用 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 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?