Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
77
rated 0 times [  84] [ 7]  / answers: 1 / hits: 19326  / 12 Years ago, sun, february 10, 2013, 12:00:00

I am trying to create a spy on a constructor, and see if it gets called -- below are my tests.
I'm using sinon-chai so the syntax is valid, but both tests fail.



var foo = function(arg) {
};

var bar = function(arg) {
var baz = new foo(arg);
};

it('foo initialized inside of this test', function() {
var spy = sinon.spy(foo);
new foo('test');
expect(spy).to.be.called;
expect(spy).to.be.calledWith('test');
});
it('foo initialized by bar()', function() {
var spy = sinon.spy(foo);
bar('test');
expect(spy).to.be.called;
expect(spy).to.be.calledWith('test');
});

More From » mocha.js

 Answers
4

The problem is that Sinon doesn't know what reference to spy on, so the solution is to either use an object i.e. sinon.spy(namespace, 'foo') or override the reference yourself foo = sinon.spy(foo).


[#80313] Friday, February 8, 2013, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
frederickmohamedw

Total Points: 21
Total Questions: 123
Total Answers: 105

Location: The Bahamas
Member since Tue, Apr 27, 2021
3 Years ago
frederickmohamedw questions
Wed, Sep 23, 20, 00:00, 4 Years ago
Sat, Jul 18, 20, 00:00, 4 Years ago
Sun, Apr 26, 20, 00:00, 4 Years ago
Sat, Jan 11, 20, 00:00, 4 Years ago
Fri, Dec 27, 19, 00:00, 4 Years ago
;