I'm struggling with testing my promise unit test.
An assertion "expect(scope.test).toBe(12);" has been added to the promise then block in my code.
Here is the snippet of code I am attempting to test:
$scope.getBudgets = function(){
BudgetService.getBudgets().then(function(response) {
$scope.test = 12;
}, function(response) {
});
}
Below is the unit test I have written:
describe('budgetOverviewCtrl tests', function() {
beforeEach(module('app'));
beforeEach(module('ngRoute'));
var ctrl, scope, deferred;
describe('budgetOverviewCtrl with test', function() {
beforeEach(inject(function($controller, _$rootScope_) {
scope = _$rootScope_.$new();
ctrl = $controller('budgetOverviewCtrl', {
$scope: scope
});
}));
it('Should verify if getBudgets service promise exists and returns as expected', inject(function($injector, $q, BudgetService) {
BudgetService = $injector.get("BudgetService");
deferred = $q.defer();
deferred.resolve({"Hello": "World"});
spyOn(BudgetService, 'getBudgets').and.callFake(function() {
return deferred.promise;
});
scope.getBudgets();
expect(BudgetService.getBudgets).toHaveBeenCalled();
**//The following line is within the promise then block.**
expect(scope.test).toBe(12);
}));
});
});