-
-
Notifications
You must be signed in to change notification settings - Fork 771
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Possibility to call an arbitrary function from stubs #118
Comments
This already exists:
|
Brilliant! And it´s right there in the documentation, too! Sorry, missed it somehow. |
I vote to reopen this because of the following: // Argument matching
var stub = sinon.stub();
stub.withArgs("a").calls(f);
stub.withArgs("b").returns(something); |
There already is a way to execute custom logic. I generally think non-trivial logic in tests is a bad idea, and don't want to encourage it with further features in this direction. |
I wouldn't say that mocking a contract in a test is a bad idea, nor that it should be non trivial. An example could be a request handler in a router: var mockRequest = sinon.stub();
mockRequest.withArgs(someReq, someRes).calls(function(req, res) {
res.send('mock result 1');
});
mockRequest.withArgs(someOtherReq, someOtherRes).calls(function(req, res) {
res.send('mock result 2');
}); This could of course be achieved with "yieldsTo", but I think that is non obvious and doesn't read very well. However, I understand that you don't want to add to an already large api. |
I also stuck with this: var mockRequest = sinon.stub();
mockRequest.onThirdCall().calls(function() {
console.log('Yes!');
done();
}); Any hints on how this can be achieved is requested. :( |
That would be very useful. For now, I have to make my own stub function for that :( |
Why there should be an object and "method" to be able to stub(fn)? I can't understand what is the difference with spy(fn)? |
+1 to @shakiba's comment. The 'answer' here, The workaround is ugly:
I'd much rather have a clean syntax like:
|
@peterflynn You can do this with a spy: var spyYouWant = sinon.spy(function () { ... }); |
@mantoni a spy doesn't let you define different behavior for different invocations. I also have a use case for |
+1 for @ChiperSoft's use case |
+1 for @ChiperSoft's proposed change |
@ChiperSoft You are perfectly capable of defining different behaviour for different invocations yourself. Just check the callcount in the spy. @bion We are trying shrink the API down for 2.0. See if you can create a wrapper/plugin to extend the core functionality if you think this is useful and publish it to NPM if it works for you. |
What I ended up doing was producing my own function stubbing library that let me define callbacks for each invocation. It makes the tests a lot simpler and it let me remove sinon from my projects entirely. http://npm.im/stepperbox |
my stub is executing the original method why |
@ppyoosuf please post usage questions to the mailing list or stack overflow, after reviewing the docs. If you find an actual issue/bug, try opening a new issue and post enough code for us to verify the issue. |
his is my controller function in node.js exports.searchDocument=function(req,res){ var type=req.query.docType; var
here docService.getDocByTypeDep is a database service function this.getDocByTypeDep=function(ser,typeId,depId,cb){ var myErr=null,data=null; con.query(qry,typeId,function(err,res){ if(err)
i wrote the test cases by sinon stub ,but it executing the original function
to be tested
On Thu, Jun 23, 2016 at 12:16 PM, Carl-Erik Kopseng <
|
@ppyoosuf We are trying to keep the GitHub issues list tidy and focused on bugs and feature discussions. This is a usage question, please post it to the Sinon.JS mailinglist, so the bigger community can help answer your questions. |
OK |
Without this feature use of fake timers is a pain. I would like to see something like:
It's achievable only through by specific construction of stub, which is not always convenient or possible:
Ability to change state of a test/environment on call is important for a lot of different tests and crucial to have in mock library. |
in sinon 2.1, |
I didn't see this solution posted above, so adding incase it helps others: Using the spy interface to get the args of the nth call
|
I'd like a function
calls(f)
on stubs that allows me to execute arbitrary code when the stub is invoked. That functionf
could get all the arguments the initial function was called with.I'll gladly provide a pull request for this, but I'd first like to make sure it has a chance of getting accepted.
The text was updated successfully, but these errors were encountered: