简体   繁体   English

Sinon匿名存根作为参数传递

[英]Sinon anonymous stub passed as a parameter

I am trying to test a function in a redux container but the issue is barely about redux or react. 我试图在redux容器中测试一个函数,但问题几乎没有关于redux或反应。 Basically the fetchData function I am trying to test, takes two functions as parameters and calls them. 基本上我试图测试的fetchData函数,将两个函数作为参数并调用它们。

What I was hoping to do was to have two anonymous stubs and pass them to the function this way: 我希望做的是有两个匿名存根,并以这种方式传递给函数:

var firstStub = sinon.stub().withArgs(mockResponse).returns('success');
var secondStub = sinon.stub().withArgs(mockResponse).returns('success');
AccountApp.fetchData({ firstStub , secondStub });

When this happens my function fetchData complains about the firstStub and secondStub not being a function. 当发生这种情况时,我的函数fetchData抱怨firstStub和secondStub不是函数。 I know they are stub objects but if that is the case what is the correct way of managing this situation. 我知道它们是存根对象,但如果是这种情况,那么管理这种情况的正确方法是什么。

Passing the stubs as 将存根传递为

AccountApp.fetchData({ firstStub , secondStub });

seems to be the culprit, because this means that you actually (after ES6 desugaring) invoke this: 似乎是罪魁祸首,因为这意味着你实际上(在ES6 desugaring之后)调用了这个:

AccountApp.fetchData({ firstStub: firstStub, secondStub: secondStub });

and this means that your fetchData() function would need to have an implementation like this: 这意味着你的fetchData()函数需要有这样的实现:

function(args) {
   // ....
   args.firstStub(params);
   args.secondStub(params);
   // ...
};

I seriously doubt that your production code refers to those callbacks as "stubs". 我严重怀疑你的生产代码将那些回调称为“存根”。 So you probably want to invoke the function like this: 所以你可能想调用这样的函数:

AccountApp.fetchData(firstStub, secondStub);

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

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