简体   繁体   English

如何使用 mocha 和 chai 正确测试使用 axios.get 和 Cheerios.load 函数的 function?

[英]How do I properly test a function that use axios.get and cheerios.load functions with mocha and chai?

I have this index.js file我有这个 index.js 文件

    const axios = require("axios");
    const cheerio = require('cheerio');

    const fetchData = async (webpage) => {
        const result = await axios.get(webpage);
        return cheerio.load(result.data);
    }
    const extractData = async (webPage) => {
        const $ = await fetchData(webPage);
        const numMains = $('.rank').text();
        //Should return the following string 
        //1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.27.28.29.30.
        return numMains;
    }

    const siteUrl = "https://news.ycombinator.com/";

    (async (webPage) => {console.log(await extractData(webPage));})(siteUrl);

    module.exports = {
        extractData: extractData
    }

And I have this test.js file我有这个 test.js 文件

    var chai = require('chai');
    var expect = chai.expect;
    var functionNames = require('../index.js');

    describe('extractData() Unit-Test', () => {
      it('This function should return a given string', () => {

        // 1. ARRANGE
        const siteUrl = "https://news.ycombinator.com/";
        const shouldResult = "1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.27.28.29.30.";

        // 2. ACT
        const result = functionNames.extractData(siteUrl);

        // 3. ASSERT
        expect(result).to.be.equal(shouldResult);

      });
    });

*I'm not getting pass with this AssertionError: * *我没有通过这个 AssertionError:*

    AssertionError: expected {} to equal '1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.27.28.29.30.'

Does someone know what I'm doing wrong and how can I solve this problem?有人知道我做错了什么,我该如何解决这个问题?

You're missing an await , you're comparing a Promise against 1,2,3 ...你错过了await ,你正在比较Promise1,2,3 ...

it('This function should return a given string', async() => {

        // 1. ARRANGE
        const siteUrl = "https://news.ycombinator.com/";
        const shouldResult = "1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.27.28.29.30.";

        // 2. ACT
        const result = await functionNames.extractData(siteUrl);

        // 3. ASSERT
        expect(result).to.be.equal(shouldResult);

});

Have in mind that you're actually performing the request while doing the test, if the site is down the test will fail.请记住,您实际上是在进行测试时执行请求,如果站点关闭,则测试将失败。 If you don't want that you can mock the request, using nock or a similar package.如果您不想这样,您可以使用nock或类似的 package 模拟请求。

As an alternative to Marcos's solution, you could also use eventually from chai:作为 Marcos 解决方案的替代方案,您还可以eventually从 chai 使用:

result.should.eventually.equal(shouldResult);

Documentation here: https://www.chaijs.com/plugins/chai-as-promised/此处的文档: https://www.chaijs.com/plugins/chai-as-promised/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何正确等待这个 axios.get 请求? - How to properly await this axios.get request? 如何在异步函数中包装Mocha / Chai测试? - How do I wrap Mocha/Chai tests within asynchronous functions? 如何测试使用Mocha,Sinn和Chai返回的函数? - How do I test a function which returns a function using Mocha, Sinon, and Chai? 如何从 beforeEach() 传递值进行测试? 摩卡/柴 - How do I pass value from beforeEach() to test? Mocha/Chai 如何测试Mocha / Chai以验证是否为变量分配了值? - How do I test Mocha/Chai to verify that a variable was assigned a value? 如何使用 mocha/chai 测试 ejs/nodejs function? - How to test ejs/nodejs function with mocha/chai? 如何使用 Mocha 和 Chai 证明函数返回的 object 是作为 function 的参数提供的参数的“克隆”? - How do I use Mocha and Chai to prove a function's returned object is a “clone” of the argument supplied as an argument to a function? TypeError: axios.get 不是 function 吗? - TypeError: axios.get is not a function? 为什么我得到 axios.get 不是一个函数? - Why am I getting axios.get is not a function? 如何使用Mocha,Chai和Sinon正确测试Express控制器方法 - How to properly test an Express controller method with Mocha, Chai, and Sinon
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM