Skip to content

Instantly share code, notes, and snippets.

@lukasz-kaniowski
Last active December 24, 2015 22:08
Show Gist options
  • Save lukasz-kaniowski/6870120 to your computer and use it in GitHub Desktop.
Save lukasz-kaniowski/6870120 to your computer and use it in GitHub Desktop.
Promise testing
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
});
});
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