JavaScript Use sinon.js to create a “spy object” with spy methods based on a real constructor/prototype

I'm using sinon.js as a way to stub out dependencies in my Mocha tests. I prefer the 'spy' approach over a classic mock approach, as the introspection of the spy seems clearer and affords more flexibility than the somewhat backward-thinking with classic mock objects.

That said, I wonder if I'm using it incorrectly when it comes to creating test spies for whole objects. Let's say I have a test dependency that has 4 methods on it and I want to stub each of those methods and make assertions on one or two of them. Currently I'm doing this:

var spyObj = {
  aMethod: sinon.spy(),
  otherMethod: sinon.spy(),
  whatever: sinon.spy()
};

Then I just ask things like spyObj.aMethod.calledWith(a, b, c).

Is there a better way to mock out an entire class than repeating the names of the methods in the test suite itself? It looks like sinon.stub() tries to iterate over all the member of a given object, but that doesn't seem to work as a way to get all methods for most objects in more modern JS runtimes, like V8, unless the object is actually something enumerable. It also tries to monkey patch the actual object, rather than returning an equivalent one, which is somewhat undesirable. I just need an object that conforms to an interface, but behaves like a null object, unless I tell it otherwise.

It would be good to be able to do something like:

var spyObject = sinon.spy(MyClass.prototype);

How does one find all methods of a constructor/prototype in Node.js, in order to make a wrapper like the above?

This is more about stubbing out logic, than testing invocations on lots of methods (which I try to limit to one, or at a push two). For example, things that might do unwanted I/O, or require additional complex fixtures if executed.

Answer:1

Starting with Sinon 1.6.0 you can do:

var stub = sinon.createStubInstance(MyClass)

See documentation for sinon.stub in Stub API section or the source.

Answer:2

I get the following error "Web Service method name is not valid" when i try to call webmethod from javascript System.InvalidOperationException: SaveBOAT Web Service method name is not valid. ...

I get the following error "Web Service method name is not valid" when i try to call webmethod from javascript System.InvalidOperationException: SaveBOAT Web Service method name is not valid. ...

  1. service method name is not valid
  2. web service method name is not valid
  3. web service method name is not valid. c#
  4. web service method name is not valid asmx
  5. web service method name is not valid vb.net
  6. web service method name is not valid json
  7. web service method name is not valid ajax
  8. web service method name is not valid. jquery asmx
  9. $metadata web service method name is not valid
  10. soap web service method name is not valid
  11. postman web service method name is not valid
  12. soaphttpclientprotocol webservice method name is not valid

I have code like the following: function test(obj) { if(//some conditon) { obj.onload(); }else{ obj.onerror(); } } for(var i=0;i<4;i++){ test({ onload:...

I have code like the following: function test(obj) { if(//some conditon) { obj.onload(); }else{ obj.onerror(); } } for(var i=0;i<4;i++){ test({ onload:...

In Three.js, I would like to use THREE.quaternion to make the camera object rotate to the selected object. I did search the web but found no example/demo or document about how to use this quaternion ...

In Three.js, I would like to use THREE.quaternion to make the camera object rotate to the selected object. I did search the web but found no example/demo or document about how to use this quaternion ...

  1. three.js rotate camera quaternion
  2. three js camera quaternion
  3. three.js rotate object quaternion

I am creating a popup window that goes to hello.html. I want my original (parent page) to reload when i close the popup window (hello.html). I can't seem to get it to work, but I'm close. Here is ...

I am creating a popup window that goes to hello.html. I want my original (parent page) to reload when i close the popup window (hello.html). I can't seem to get it to work, but I'm close. Here is ...

  1. refresh parent window after closing modal dialog
  2. refresh parent window after closing popup
  3. reload parent window after closing popup
  4. javascript refresh parent window after closing popup
  5. auto refresh parent window after closing popup javascript
  6. refresh parent window after closing child window javascript
  7. refresh parent page after closing popup window
  8. refresh parent page after closing modal dialog