简体   繁体   English

测试Promise链中未公开的功能

[英]Testing unexposed functions in a Promise chain

I have UI component that calls a method getUserInfo() which returns a chain of promises. 我有UI组件,该组件调用方法getUserInfo() ,该方法返回承诺链。 When I first built this I thought it was a good idea, and I wrote a bunch of code without test. 当我第一次构建它时,我认为这是一个好主意,我编写了许多未经测试的代码。 getUserInfo() takes a couple of input parameters and returns the following. getUserInfo()接受几个输入参数,并返回以下内容。 It is the beginning of a long chain of promises that resolves to an array of strings. 这是一长串的诺言的开始,最终解决了一个字符串数组。 Now I'm trying to test but I'm having quite a bit of trouble ... 现在我正在尝试测试,但遇到了很多麻烦...

utilities.js utilities.js

...
// function geocodeLocation(zipcode){...};
// function getPlace(coordinates){...};
// ... etc
...

const utilities = {
    getUserInfo: function (zipcode, gender) {
        return geocodeLocation(zipcode)
            .then(coordinates => getPlaces(coordinates))
            .then(placeData => getDistanceFromEachPlace(origin, placeData))
            .then(places => getMembersFromPlaces(places, gender))
            .then(members => sortMembersByAge(members))
            .then(sortedMembers => selectMembers(sortedMembers, gender))
    }
};

module.exports = utilities;

getUserInfo is located within a utilities module and is the only function exposed. getUserInfo位于utilities模块内,是唯一公开的功能。 I'm a bit lost when it comes to testing this. 在测试这个方面我有点迷茫。 getUserInfo is the start of a long chain of functions. getUserInfogetUserInfo功能的开始。 Most of the functions that are chained make external API calls, some of which are wrapped around promises. 链接的大多数功能都进行外部API调用,其中一些包装在promises中。 If the functions aren't making external API calls, they are munging data and creating new data sets (in other words, functions that should be unit tested). 如果这些函数没有进行外部API调用,则它们是在处理数据并创建新的数据集(换句话说,应该进行单元测试的函数)。 Stubbing and mocking seems appropriate here, but I get confused as to what to stub out and what to mock. 在这里进行存根和嘲笑似乎很合适,但是对于存根和嘲笑什么我感到困惑。 Should I mock out each of the functions, and test end result of the promise? 我应该模拟每个功能,并测试promise的最终结果吗?

Also, since getUserInfo is the only function exposed in the module, individual unit testing does not seem feasible here. 另外,由于getUserInfo是模块中公开的唯一函数,因此此处进行单个单元测试似乎不可行。 How can i go about testing all the intermediary functions like getDistanceFromEachPlace when they aren't exposed? 不公开时如何测试所有中间函数(如getDistanceFromEachPlace)?

testfile.js testfile.js

import utilities from '../util/utilities'
import sinon from 'sinon'
import mocha from 'mocha'

describe('___api', () => {
  it('should return a promise', () => {
    const zipcode = '90210'
    const gender = 'male'
    // stub( other api functions here?? )
    expect(utilities.getUserInfo(zipcode, gender).to.be.a('promise')
  });
});

Can I even test this thing now? 我现在可以测试一下吗?

This is a opinionated answer, so take from it what you will. 这是一个自以为是的答案,因此请您从中获取帮助。

The basis of my answer is that if you have to change the code under test to actually test it, then your not actually testing the code that you want. 我回答的基础是,如果您必须更改要测试的代码以对其进行实际测试,那么您就不必实际测试所需的代码。


Should I mock out each of the functions, and test end result of the promise? 我应该模拟每个功能,并测试promise的最终结果吗?

I would focus more on mocking whatever you have used to make the external API calls. 我将重点放在模拟用来进行外部API调用的内容上。 That is, keep utilities.js unchanged and in your test code use sinon to mock the library/code you have used to make the external API calls. 也就是说,保持utilities.js不变,并在测试代码中使用sinon嘲笑你已经使用,使外部API调用的库/代码。 (I can't really give you an example how to do this because I don't know what you are using) (我无法真正给您一个示例,因为我不知道您在使用什么)

How can i go about testing all the intermediary functions like getDistanceFromEachPlace when they aren't exposed? 不公开时如何测试所有中间函数(如getDistanceFromEachPlace)?

I wouldn't - unit testing should test through the interface of the piece of code. 我不会-单元测试应该通过代码的界面进行测试。


In saying all that, if you want to look into changing the internals of your module, rewire is a module which will allow you to do that. 说了这么多,如果您想研究更改模块的内部结构,那么rewire是一个允许您执行此操作的模块。

https://github.com/jhnns/rewire https://github.com/jhnns/rewire

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM