问题
I am writing a unit test for my Angular application using Jasmine with a mock service. However, the promise I am trying to run is not working.
Here's the service:
CreateItemController = $controller('CreateItemController', {
ItemService: {
createItem: function(data) {
console.log('Service running');
var defer = $q.defer();
defer.resolve('1');
return defer.promise;
}
The test:
it('should create an item', function() {
var created = false;
$scope.createItem().then(function(response) {
// This is not being run
console.log("We got through");
created = true;
});
expect(created).toBe(true);
})
And finally the actual function:
$scope.createItem = function() {
var postData = {
name: 'Jeans'
};
return ItemService.createItem(postData).then(function(response) {
// This is not being run
console.log('Promise recieved');
});
}
What am I doing wrong?
回答1:
You should mock the $q service so that the resolution of the promise is not async anymore in your test.
There is no point testing the $q service itself, you only want to test your custom logic.
describe('controller: CreateItemController', function () {
var subject, $scope;
beforeEach(module('myModule'));
beforeEach(inject(function ($rootScope, $controller, $q) {
$scope = $rootScope.$new();
subject = $controller('CreateItemController', {
$scope: $scope,
ItemService: {
createItem: function () {
var defer = $q.defer();
spyOn(defer.promise, 'then').andCallFake(function (callback) {
callback();
return this;
});
return defer.promise;
}
}
});
}));
it('should create an item', function () {
var created = false;
var subject = $scope.createItem().then(function () {
created = true;
});
//deferred.resolve('1'); // useless as our callbacks are called as soon as they are registered
expect(created).toEqual(true);
});
});
If you want to keep the delay though, you could call $scope.$apply() after resolving the deferred object as follow:
describe('controlle: stack overflow', function () {
var subject, $scope, deferred;
beforeEach(module('myModule'));
beforeEach(inject(function ($rootScope, $controller, $q) {
$scope = $rootScope.$new();
subject = $controller('CreateItemController', {
$scope: $scope,
ItemService: {
createItem: function () {
deferred = $q.defer();
return deferred.promise;
}
}
});
}));
it('should create an item', function () {
var expected = {
one: false,
two: false,
};
var subject = $scope.createItem();
subject.then(function () {
expected.one = true;
}).then(function () {
expected.two = true;
});
deferred.resolve();
expect(expected.one).toEqual(false);
expect(expected.two).toEqual(false);
$scope.$apply();
expect(expected.one).toEqual(true);
expect(expected.two).toEqual(true);
});
});
});
来源:https://stackoverflow.com/questions/24264161/promise-is-not-being-handled