Update: I noticed that the problem is related to the promise. My understanding is that the only way to do this is to stub the entire model like this: var stub = sinon. The test not coverage line under sm.callSoap function Documents are instances of our model. When testing your front end code, you may need to test more complex behavior such as making asynchronous requests. Therefore, it will always be undefined if you’re using your model to mock a save(). sinon-as-promised by bendrucker - Sugar methods for sinon.js stubs for working with promises Creating them and saving to the database is easy. However, it is bad practice to make actual requests in a test, so you have to find a way to test your code without sending real requests. That’s where sinon comes in. Sinon is a powerful tool, and, by following the practices laid out in this tutorial, you can avoid the most common problems developers run into when using it. save is not a method on the model, it's a method on the document (instance of a model). Documents are instances of our model. prototype); I want to create a stub for the Mongoose save method in a particular model, so that any instance of my model I create will call the stub instead of the normal Mongoose save method. Something like sinon.stub(Image.prototype, 'save').resolves(theCallingObject); Is this possible? And if you're using karma to run your tests there's even a plugin for that karma-sinon-stub-promise. Sinon is resolving the request and I am using await mongodb.connect(); but it is not working as expected, and if I remove await and return value instead of promise then it works. Next, it looks like you're using sinon-as-promised, but your code doesn't call stub.resolves the same way the docs does, so I'm just assuming. View more quick examples below, or dive into the API docs, which also provides useful pointers on how and when to use the various functionality. Is there a way to stub the save method for Image to resolve the object which called it? Dismiss Join GitHub today. In this Sinon tutorial, Jani Hartikainen demonstrates how to make unit testing non-trival JavaScript code trivial with the help of spies, stubs and mocks. After that, you just have to the if the Promise will resolve and reject. stub (myModel. It works as a standalone, however. Basically, when calling sinon.stub().resolves(...) as part of the second argument of sinon.createStubInstance(), I get undefined. Creating them and saving to the database is easy. Very simple, one just have to stub the function that will return the Promise, use the function returnsPromise. You’ve seen the most common tasks people tackle with Sinon.JS, yet we’ve only scratched the surface. Stated here in mongoose docs.. Constructing documents. Any help is appreciated. Without it, your test will not fail when the stub is not called. Stated here in mongoose docs.. Constructing documents. Therefore, it will always be undefined if you're using your model to mock a save(). Stack Overflow; IRC: #sinon.js on … save is not a method on the model, it’s a method on the document (instance of a model). Get help. I want image in the then clause to just be the same as newImage. Describe the bug This was working in 7.3.2, but broke in 7.4.1. Put together, it's hard to tell exactly why your stub isn't working without a more concise and correct example. Conclusion. GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together. ie. Oh yeah! Sugar methods for sinon.js stubs for working with promises - a JavaScript package on Bower - Libraries.io i’m trying test a es6 class, but i don’t know how stub a function module whit sinon. Thanks! Although we used DOM objects as an example here, you can apply these same methods to stub any kind of more complex object. Dealing with complex objects in Sinon.js is not difficult, but requires you to apply different functionality together to make things work.