stub to capture console.log returns TypeError: Should wrap property of object

1,312 views
Skip to first unread message

Dave Collins

unread,
Nov 6, 2015, 10:19:53 AM11/6/15
to Sinon.JS
I'm new to sinon and not entirely sure how it is meant to be implemented.

I am using it in a Qunit test to intercept a message displayed to the console.

I have a popup.js utility that calls console.log('Error!!!') when the popup'd message is missing.
In my popup.tests.js file I'm trying to do this:

    QUnit.test('A warning message is logged to console when no message is provided to Popup', function (assert) {
        delete config.message; // Remove the message from Popup config (message is required).
        // openPopup();// Call the popup's open function. With no message, this will log to console

        var stub = sinon.stub(Popup, "openPopup"); // the open method for Popup utility
        var intercepted = stub.returns(Popup); // intercepted should contain "Error!!!"
    });

My first problem is that there's no object in my test file called Popup. I don't know how to get a handle on it.

This is what I've tried:
    var p = QUnit.module('Popup.js', {
        ...
    });


then
    QUnit.test('warning message logged', function (assert) {

       
var stub = sinon.stub(p, "openPopup");
       
var intercepted = stub.returns(p);
   
});

but I still get TypeError: Should wrap property of object

Message has been deleted
Message has been deleted

Dave Collins

unread,
Nov 6, 2015, 11:27:10 AM11/6/15
to Sinon.JS
OK, I got an answer. 
I'm not supposed to be referencing my utility, I'm supposed to be referencing console itself.
Then I can compare the output using calledWith (a spy method that stub inherits).

QUnit.test('A warning message is logged to console when no message is provided to Popup', function (assert) {

 
delete config.message; // Remove the message from Popup config (message is required).

 
var myStub = this.stub(console,"log");
  openPopup
();// Call the popup's open function. With no message, this will log to console

 
assert.strictEqual(myStub.calledWith('Error!!!'), true, 'A warning should be sent to console');// test what I expected versus what I got
});

Reply all
Reply to author
Forward
0 new messages