I'm trying to test a JavaScript object with Mocha using chai, chai-as-promised, sinon, sinon-chai and sinon-as-promised (with Bluebird).
Here is the object under test:
function Component(MyService) {
var model = this;
model.data = 1;
activate();
function activate() {
MyService.get(0).then(function (result) {
model.data = result;
});
}
}
and here is the test:
describe("The object under test", function () {
var MyService, component;
beforeEach(function () {
MyService = {
get: sinon.stub()
};
MyService.get
.withArgs(0)
.resolves(5);
var Component = require("path/to/component");
component = new Component(MyService);
});
it("should load data upon activation", function () {
component.data.should.equal(5); // But equals 1
});
});
My problem is I don't have a hold on the promise used in the component to wait for it before checking with the ways described in the docs of Mocha, sinon-as-promised.
How can I make this test passing?