admin管理员组文章数量:1388072
So basically I have an function whose behavior I want to stub only if the argument is equal to something. Example
var sinon = require('sinon');
var foo = {
bar: function(arg1){
return true;
}
};
var barStub = sinon.stub(foo, "bar");
barStub.withArgs("test").returns("Hi");
// Expectations
console.log(foo.bar("test")); //works great as it logs "Hi"
// my expectation is to call the original function in all cases except
// when the arg is "test"
console.log(foo.bar("woo")); //doesnt work as it logs undefined
I am using this package
So basically I have an function whose behavior I want to stub only if the argument is equal to something. Example
var sinon = require('sinon');
var foo = {
bar: function(arg1){
return true;
}
};
var barStub = sinon.stub(foo, "bar");
barStub.withArgs("test").returns("Hi");
// Expectations
console.log(foo.bar("test")); //works great as it logs "Hi"
// my expectation is to call the original function in all cases except
// when the arg is "test"
console.log(foo.bar("woo")); //doesnt work as it logs undefined
I am using this package https://www.npmjs./package/sinon
Share Improve this question edited Jul 22, 2015 at 22:47 Gepser Hoil 4,2465 gold badges25 silver badges36 bronze badges asked Jul 22, 2015 at 22:35 GyandeepGyandeep 13.6k5 gold badges32 silver badges42 bronze badges 1- Where do you define sinon? – nril Commented Jul 22, 2015 at 22:41
2 Answers
Reset to default 8I had the same problem. The accepted answer is outdated.
stub.callThrough();
will acheive this.
just call barStub.callThrough()
after barStub.withArgs("test").returns("Hi")
https://sinonjs/releases/v7.5.0/stubs/
Looking around:
https://github./cjohansen/Sinon.JS/issues/735 https://groups.google./forum/#!topic/sinonjs/ZM7vw5aYeSM
According to the second link, Christian writes:
Not possible, and should mostly not be necessary either. Your options are:
- Simplifying your tests to not cover so many uses in one go
- Express the desired behavior in terms of withArgs, returns/yields etc
- Use
sinon.stub(obj, meth, fn)
to provide a custom function
I'd be inclined to try out option 3 - see if you can get it to work (the documentation is really light unfortunately).
本文标签: javascriptStub a function for only one argumentStack Overflow
版权声明:本文标题:javascript - Stub a function for only one argument - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744492926a2608846.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论