I want to write test for metadataLoaded
promise, sinon version is 4.1.2.
Promise resolve is called, but I don't know how to write proper test assert. Two asserts I've tied has failed.
_onObjectMatched : function (oEvent) {
var sObjectId = oEvent.getParameter("arguments").objectId;
this.getModel().metadataLoaded().then( function() {
var sObjectPath = this.getModel().createKey("TaskSet", {
id : sObjectId
});
}.bind(this));
},
QUnit.test("_onObjectMatched", function(assert) {
var oEventStub = {
getParameter: sinon.stub().returns({objectId: "1"})
};
this.oModelStub = {
createKey: sinon.stub().returns("key"),
metadataLoaded : jQuery.noop
};
sinon.stub(this.oModelStub, "metadataLoaded").resolves();
this.oController._onObjectMatched(oEventStub);
//Error: assert before promise resolves
assert.ok(this.oModelStub.createKey.calledOnce, "createKey called");
//Error: this.oModelStub.metadataLoaded.then is not a function
this.oModelStub.metadataLoaded.then(function() {
assert.ok(this.oModelStub.createKey.calledOnce, "createKey called");
});
});
Is "this" in method and in the test case pointing to the same object? It looks like it's not. Anyway, this code should work:
QUnit.test("_onObjectMatched", function(assert) {
// ...
// where obj - reference to the object with "_onObjectMatched" method
sinon.stub(obj, "_onObjectMatched").returns({
createKey: sinon.stub().returns("key"),
metadataLoaded: function () {
return Promise.resolve();
}
});
// ...
});