简体   繁体   English

节点,使用sinon进行单元测试和模拟

[英]node, unit-testing and mocking with sinon

So I am using a test suite of Chai, rewire, sinon, and sinon-chai to test some node javascript. 因此,我正在使用Chai,rewire,sinon和sinon-chai的测试套件来测试某些节点javascript。 This is my first time trying to set this up so I could use some pointers. 这是我第一次尝试进行设置,因此可以使用一些指针。 The function I am trying to test looks like so : 我要测试的功能如下所示:

UserRoles.get = function(ccUrl, namespace, environment, ccToken, authPath) {
    var crowdControl = new CrowdControl(ccUrl, namespace, environment, ccToken, authPath);
    return q.Promise(function(resolve, reject) {
        crowdControl.get().then(resolve).fail(reject).done();
    });
};

Inside a document that exports as UserRoles. 在导出为UserRoles的文档中。 So I have the initial set up working fine, where I am having troubles is mocking to test this function. 因此,我的初始设置工作正常,遇到麻烦时正在模拟测试此功能。 I'm trying to mock the new CrowdContol part so my attempt to do that looks like so : https://jsfiddle.net/d5dczyuk/ . 我正在尝试模拟新的CrowdContol部分,因此我的尝试看起来像这样: https ://jsfiddle.net/d5dczyuk/。

so I'm trying out the 所以我正在尝试

testHelpers.sinon.stub(CrowdControl, "UserRoles");

to intercept and stub 拦截和存根

var CrowdControl = require('./crowdcontrol');

then just running 然后就跑

userRoles.get;

console.log(CrowdControl);

And it seems the stub is not being called ( it logs it's a stub but not that it has been called). 似乎未调用存根(它记录它是存根,但未调用它)。 I will also need to stub the crowdControl.get() hopefully too, however I was trying to get this simple part working first. 我也同样希望也需要打败pullControl.get(),但是我试图首先让这个简单的部分开始工作。 Not sure what I need to be doing differently to get this to work here. 不知道我需要做些什么来使它在这里工作。 This is my first time unit testing in node, I've done a bunch in angular where I could just "mock" the CrowdControl, but I'm not sure how it works in node. 这是我第一次在节点中进行单元测试,我在角度上做了很多工作,可以“模拟” CrowdControl,但是我不确定它如何在节点中工作。

Just to clarify I am just checking if CrowControl will be called with those vars passing in, should I just stub it? 只是为了澄清一下,我只是在检查是否将通过传入的var调用CrowControl,我是否应该将其存根? but I also want to mock the crowdControl so I can force what the get returns. 但是我也想嘲笑一下crowdControl,这样我就可以强制get得到什么。

Edit: here is my second attempt : https://jsfiddle.net/5m5jwk5q/ 编辑:这是我的第二次尝试: https : //jsfiddle.net/5m5jwk5q/

I like to use proxyquire for this kind of testing. 我喜欢使用proxyquire进行这种测试。 With proxyquire you can stub out require'd dependencies from the modules you're trying to test. 使用proxyquire,您可以从要测试的模块中找出需要的依赖项。 So in your case you could do: 因此,您可以执行以下操作:

var crowdControlSpy = sinon.spy();

// Makes sure that when ./user-roles tries to require ./crowdcontrol
// our controlled spy is passed, instead of the actual module.
var UserRoles = proxyquire('./user-roles', {
    './crowdcontrol': crowdControlSpy
});

UserRoles.get(...);
expect(crowdControlSpy).to.have.been.called;

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

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