Last active
December 24, 2015 22:08
-
-
Save lukasz-kaniowski/6870120 to your computer and use it in GitHub Desktop.
Promise testing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var customerService, deferredCustomer, sampleCustomer, | |
productService, deferredProduct, sampleProduct; | |
beforeEach(function () { | |
sampleCustomer = {name:'Bob'}; | |
customerService = { get:sinon.stub() }; | |
productService = { get:sinon.stub() }; | |
sampleProduct = { type: 'Product'} | |
module(function ($provide) { | |
$provide.value('customer', customerService); | |
$provide.value('product', productService); | |
}); | |
inject(function (_$q_, _$rootScope_) { | |
$rootScope = _$rootScope_; | |
deferredCustomer = _$q_.defer(); | |
deferredProduct = _$q_.defer(); | |
}); | |
}); | |
it('should resolve multiple promises', function () { | |
given(function () { | |
customerService.get.returns(deferredCustomer.promise); | |
productService.get.withArgs(user: 'Bob').returns(deferredProduct.promise); | |
}); | |
when(function () { | |
// invoke code under test | |
deferredCustomer.resolve(sampleCustomer); | |
deferredProduct.resolve(sampleProduct); | |
$rootScope.$digest(); // this will resolve both of the promises at the same time | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var customerService, deferredCustomer, sampleCustomer; | |
beforeEach(function () { | |
sampleCustomer = {name:'Bob'}; | |
customerService = { get:sinon.stub() }; | |
module(function ($provide) { | |
$provide.value('customer', customerService); | |
}); | |
inject(function (_$q_, _$rootScope_) { | |
$rootScope = _$rootScope_; | |
deferredCustomer = _$q_.defer(); | |
}); | |
}); | |
it('should resolve promise', function () { | |
given(function () { | |
customerService.get.returns(deferredCustomer.promise); | |
}); | |
when(function () { | |
// invoke code under test | |
deferredCustomer.resolve(sampleCustomer); | |
$rootScope.$digest(); | |
}); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment