我正在使用带有模拟服务的 Jasmine 为我的 Angular 应用程序编写单元测试。但是,我试图运行的承诺无效。
这是服务:
CreateItemController = $controller('CreateItemController', {
ItemService: {
createItem: function(data) {
console.log('Service running');
var defer = $q.defer();
defer.resolve('1');
return defer.promise;
}
测试:
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);
})
最后是实际功能:
$scope.createItem = function() {
var postData = {
name: 'Jeans'
};
return ItemService.createItem(postData).then(function(response) {
// This is not being run
console.log('Promise recieved');
});
}
我做错了什么?
答案 0 :(得分:0)
您应该模拟$ q服务,以便在您的测试中,承诺的解决方案不再异步。
没有必要测试$ q服务本身,你只想测试自定义逻辑。
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);
});
});
如果您想保持延迟,可以在解析延迟对象后调用$ scope。$ apply(),如下所示:
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);
});
});
});