Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
How to mock/stub a constructor
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  2 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Jean-Philippe Poulin de Courval  
View profile  
 More options Jul 25 2012, 11:11 am
From: Jean-Philippe Poulin de Courval <jeep...@gmail.com>
Date: Wed, 25 Jul 2012 08:11:58 -0700 (PDT)
Local: Wed, Jul 25 2012 11:11 am
Subject: How to mock/stub a constructor

Hi everyone, I'm a newbie to sinonjs and I need help. I want to know, how
can I mock the constructor function of an class? For example, if I have
this code:

var MyObject = function() {
 // constructor

};

MyObject.prototype.aMethod = function() { ... };

How can I mock this object to replace the constructor with my own function?
So, when I will create a new object by doing var aObject = new MyObject();
it will not call the real constructor but only the function I provided.

I've tried this without success:

var mock = sinon.mock(MyObject.prototype);
mock.expects('constructor').returns({ Dispose: function() {} });

And also:
var mock = sinon.mock(MyObject.prototype);
mock.expects('constructor').returns({ Dispose: function() {} });

So, how can I do that? Is it possible? What is the correct way to do
something like this?
Thanks for your help!


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jean-Philippe Poulin de Courval  
View profile  
 More options Jul 25 2012, 4:10 pm
From: Jean-Philippe Poulin de Courval <jeep...@gmail.com>
Date: Wed, 25 Jul 2012 13:10:54 -0700 (PDT)
Local: Wed, Jul 25 2012 4:10 pm
Subject: Re: How to mock/stub a constructor

I finally found a hack for this problem. I created a new class that extends
the one I want to override the constructor. My code looks like this:

var MyObject = function() {
 // constructor

};

My setup method in my test have this in:
var tmp = MyObject;
var ExtendedMyObject = function() {};
goog.inherits(ExtendedMyObject, MyObject);
MyObject = ExtendedMyObject;

And now my teardown:
MyObject = tmp;

You probably noticed that I'm using Google Closure. It provides a easy way
to manage dependencies and inheritance. I was already using it in my
project so I kept using it in this case.
Feel free to submit a better response to my initial problem.

Also, why SinonJs doesn't override the constructor (if it's the right name
for this) of an object like this? Is there a reason?
Thank you!

Le mercredi 25 juillet 2012 11:11:58 UTC-4, Jean-Philippe Poulin de Courval
a écrit :


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »