Skip to content


Subversion checkout URL

You can clone with
Download ZIP


Stubbing on prototype? #206

jockster opened this Issue · 2 comments

3 participants


Is it possible to stub a "class" looking like below with sinon's own syntax, without instantiating the class?
I would very much like to stub both the constructor as well as methods:

var UserModel = (function() {
  var User;
  User = function() {}; // Constructor
  User.prototype.isValid = function() {};
  return User;

you could stub the methods on the prototype (sinon.stub(UserModel.prototype, 'isValid')) but not the constructor itself. however, instead of stubbing the constructor, you could pass a different constructor to the code that consumes it. this would require writing your code in a way that allows you to inject dependencies. i would think though it would be more typical to inject instances rather than constructors and i find it hard to think of a case where injecting a constructor is better than injecting an instance.


I guess sinon.createStubInstance(constructor) is what was asked for here (see Closing issue.

@mantoni mantoni closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.