Mein CompanyService
ist:Wie verspotten Sie das Ergebnis eines Versprechens in einem AngularJS-Komponententest?
angular.module('mean').service('CompanyService', ['$http', '$rootScope', '$q', function($http, $rootScope, $q) {
var company = this;
var initializedDeferred = $q.defer();
company.company_data = {}
company.initialized = initializedDeferred.promise;
company.getCompany = function() {
return company.company_data;
}
company.get = function (company_id) {
return $http({
url: '/api/v1/company/' + company_id,
method: 'GET',
headers: {
api_key: $rootScope.api_key
}
}).success(function(response) {
if(response.status === 'ok') {
company.company_data = response.company;
initializedDeferred.resolve();
} else {
alert('TBD: Need fail case');
}
});
};
}]);
Mein Controller ist:
angular.module('mean').controller('LocationController', ['$scope', '$location', '$rootScope', 'LocationService', 'UserService', 'CompanyService', '$modal', '$routeParams', function ($scope, $location, $rootScope, LocationService, UserService, CompanyService, $modal, $routeParams) {
$rootScope.menuItem = 'locations';
$scope.contentTemplate = '/views/location/index.html';
$scope.locations = [];
$scope.current_location = null;
$scope.newLocation = {};
$scope.location_parent_id = $routeParams.location_parent_id;
$scope.index = function() {
CompanyService.initialized.then(function() {
$scope.test = 'a';
var company_id = CompanyService.getCompany()._id;
LocationService.list(company_id, $routeParams.location_parent_id).then(function(response) {
if(response.data.status === 'ok') {
$scope.locations = response.data.locations;
$scope.current_location = response.data.location || null;
}
});
});
}
$scope.addLocationModal = function() {
$scope.location_types = ['warehouse', 'section', 'row', 'shelf', 'bin'];
$modal({
scope: $scope,
template: '/views/location/addLocationModal.html',
show: true,
animation: 'am-fade-and-scale'
});
}
$scope.createLocation = function() {
$scope.newLocation.company_id = CompanyService.getCompany()._id;
LocationService.create($scope.newLocation).then(function(response) {
if(response.data.status === 'ok') {
$scope.$hide();
} else {
alert('TBD');
}
});
}
}]);
Mein Test ist:
(function() {
describe('LocationController', function() {
var $scope, $location, $rootScope, $modal, deferred, CompanyService, createController;
beforeEach(module('mean'));
beforeEach(inject(function($injector) {
$location = $injector.get('$location');
$rootScope = $injector.get('$rootScope');
$modal = $injector.get('$modal');
$scope = $rootScope.$new();
var $controller = $injector.get('$controller');
var $q = $injector.get('$q');
var params = {
'$scope': $scope,
CompanyService: jasmine.createSpyObj('CompanyService', ['initialized'])
}
params.CompanyService.initialized.andCallFake(function() {
deferred = $q.defer();
return deferred.promise;
});
createController = function() {
return $controller('LocationController', params);
};
}));
it('should instantiate initial variables at the top level', function() {
var controller = createController();
$location.path('/company/locations');
expect($location.path()).toBe('/company/locations');
expect($rootScope.menuItem).toBe('locations');
expect($scope.contentTemplate).toBe('/views/location/index.html');
expect($scope.locations.length).toEqual(0);
expect($scope.current_location).toBeNull();
expect($scope.newLocation).toBeDefined();
expect($scope.location_parent_id).not.toBeDefined();
});
it('should list all locations for this company', function() {
var controller = createController();
deferred.resolve();
$scope.index();
expect($scope.test).toBe('a');
});
});
})();
Aber die resolve
nicht funktioniert. Ich erhalte diesen Fehler: TypeError: 'undefined' is not an object (evaluating 'deferred.resolve')
Irgendwelche Hilfe?
In Ihrem letzten Test in der 'Test' Teil des Codes haben Sie 'es sollte alle Standorte für diese Firma auflisten', Sie haben eine aufgeschobene dort nie erstellt und versuchen, ca Ich löse es auf, es ist undefiniert. (Welches ist genau das, was der Fehler dir gesagt hat). –
versuchen Sie es mit 'sinon.js', es ist ein fantastisches Spott Framework. – Gyandeep
@BenjaminGruenbaum - Ich bin mir nicht ganz sicher, was du meinst. Ist das nicht "aufgeschoben" im Rahmen der gesamten Suite, weil ich "var deferent" im obersten Bereich bin? – Shamoon