简体   繁体   English

使用Mocha js对具有多个“地图”的功能进行测试

[英]Testing using Mocha js for a function with multiple “map”

Below code is only for rough explanation; 以下代码仅作粗略解释; As you can see we need multiple returns for each "map" function to get a return from js; 如您所见,每个“ map”函数都需要多个返回值才能从js获得返回值;

How does one test this function using mocha? 如何使用Mocha测试此功能? ie we cant place multiple returns just to get the value for our testing purposes because such returns will disturb the actual implementation of said function. 即我们不能放置多个返回值只是为了获得用于测试目的的值,因为这样的返回值会干扰所述函数的实际实现。

So I need to avoid unnecessary returns for implementation sake but need the same multiple returns for testing.. 因此,为了实现实现,我需要避免不必要的回报,但需要相同的多个回报进行测试。

Suggestions please? 有什么建议吗?

 var array = ["a","b","c"], array1 = ["1","2","3"], array2 = [], returnVar; function test() { return ("done") array.map(function(data) { return ("done") array1.map(function(datum){ array2.push(datum) return("done") }); }); } returnVar = test(); alert(returnVar) 

This is not testable code, therefore it is hard for it to create test in current shape. 这不是可测试的代码,因此很难以当前形状创建测试。

map is used for data transformations, therefore you would isolate your data transformation into function with input and output value. map用于数据转换,因此您会将数据转换隔离为具有输入和输出值的函数。 Testing that function would be matter of providing input, calling function and verification of output. 测试该功能将与提供输入,调用功能和验证输出有关。

您可以存根Array.prototype.map方法,但更好的方法是像@luboskrnac所建议的那样,测试传递给map函数。

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

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