Skip to content

Instantly share code, notes, and snippets.

@NicholasBoll
Created August 18, 2015 18:57
Show Gist options
  • Save NicholasBoll/0ca7d14d4216498418ed to your computer and use it in GitHub Desktop.
Save NicholasBoll/0ca7d14d4216498418ed to your computer and use it in GitHub Desktop.
Async AngularJS testing
describe('getFormattedCustomerInfo-async', function(){
var customerService;
var customerFormattingService;
var $q;
var $scope;
beforeEach(function(){
module('customer');
inject( function($injector){
customerService = $injector.get('customer-service');
customerFormattingService = $injector.get('customer-formatting-service-async');
$q = $injector.get('$q');
$scope = $injector.get('$rootScope').$new();
});
});
it('should return formatted customer information based on async call', function(){
spyOn(customerService,['getCustomerById']).and.returnValue($q.when({firstName:'Joe',lastName:'Smith',totalSales:50}));
var formatted;
customerFormattingService.getFormattedCustomerInfo(1).then(function(_formatted){
formatted = _formatted
});
$scope.$apply();
expect(customerService.getCustomerById).toHaveBeenCalledWith(1);
expect(formatted).toBe('Joe Smith Total Sales: $50');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment