Is it possible to get sinon to resolve an argument, when the argument is a defered object?
Consider this:
function needToTest() {
var isInitialized = q.defer();
var importantResult;
var publicStuff;
publicStuff.isInitialized = isInitialized.promise.then(function(res) {
importantResult = res;
});
var someClass = new SomeClass(isInitialized);
publicStuff.getResult = function() {
return importantResult;
};
return publicStuff;
}
I use q as promise library, but it's not important. SomeClass looks something like this:
function SomeClass(promise) {
this.foo = function() {
//
};
//after some initializing:
var result = true; //or false
promise.resolve(result);
}
It might be that this mock should have some sort of function to resolve a promise, but this is what I have so far:
var someClassMock = sinon.stub().withArgs(q.defer());
someClass.prototype.foo = sinon.stub().returns('foo');
return someClassMock;
and ultimately I try to create a testcase using sinon and Squire like this:
describe('test', function() {
var needToTestInstance;
beforeEach(function(done) {
new Squire()
.mock('someClassMock', someClassMock)
.require(['needToTest'], function(module) {
needToTest = module;
//Need to get the needToTest().isInitialized to resolve with a value!
done();
});
});
describe('importantResult', function() {
expect(needToTestInstance.getResult()).to.be(true);
});
});
Is it possible to get isInitialized to resolve at any point in the test?