I'm new to unit testing with Angular and I'm having a great deal of trouble even getting starting with the testing. I have a controller that calls a service that calls a remote api to get a list of currencies, and then augments the returned object with a symbol property and assigns it to a scope variable.
I've been looking at a lot of httpBackend tutorials but the pieces aren't fitting together. I know I have to use whenGet and expectGet but still don't understand what they are and when I should use them, or even what I should be testing - the request, the response? Should they be in the beforeEach function of the it function?
the controller:
whiskyControllers.controller('currencyCtrlr', ['$scope', 'CurrencyConversion', 'UpdateMiniBasket', 'whiskyList',
function($scope, CurrencyConversion, UpdateMiniBasket, whiskyList){
$scope.getCurrencies = function(e){
e.preventDefault();
CurrencyConversion.async().then(function(d) {
$scope.rates = d.data.rates;
for(var i = 0, j = $scope.rates.length; i < j; i++){
$scope.rates[i]['symbol'] = insertSymbol($scope.rates[i].to);
}
});
}
}]);
I've copied the bulk of the test below from another tutorial but it still fails with 'Error: No pending request to flush'
the test:
describe('currencyCtrlr: getCurrencies', function() {
var scope, CurrencyConversion, UpdateMiniBasket, whiskyList, $httpBackend, $rootScope, createController,
url = 'http://ift.tt/1D4B8HL';
beforeEach(module('whiskyControllers'));
beforeEach(function () {
module(function ($provide) {
$provide.value('CurrencyConversion', CurrencyConversion);
$provide.value('UpdateMiniBasket', UpdateMiniBasket);
$provide.value('whiskyList', whiskyList);
});
});
beforeEach(inject(function($injector) {
//scope = $rootScope.$new();
$httpBackend = $injector.get('$httpBackend');
$httpBackend.when('GET', url).respond({}, {});
$rootScope = $injector.get('$rootScope');
var $controller = $injector.get('$controller');
createController = function() {
return $controller('currencyCtrlr', {'$scope' : $rootScope });
};
}));
it('should make an ajax call to http://ift.tt/1EoBq0B', function() {
$httpBackend.expectGET(url);
var controller = createController();
$httpBackend.flush();
});
afterEach(function() {
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
});
});
Any help will be really appreciated
Aucun commentaire:
Enregistrer un commentaire