2
嗨我想知道如果任何人都可以告诉我如何测试回调结果在模拟测试角和茉莉花2.我们有一个服务,只是返回一些数据的调用而像这样:角然后回调在模拟茉莉花测试
var getStuff = function (id) {
return $http.get("url/" + id)
.then(function (response) {
return response.data;
});
};
,并在我们的控制,我们称这种服务在启动时:
angular.module('controllers').controller("myController", ['$scope', 'service', function ($scope, service) {
var onGetStuff = function(data) {
//do something with the result here
}
$scope.getItems = function() {
service.getStuff(1).then(onGetStuff);
}
$scope.getItems();
}]);
我然后运行我的测试(会后的代码在一个时刻,而是调用onGetStuff从来没有被称为,我似乎无法弄清楚为什么。以下是测试代码:
//includes here
describe('Controller: Applicant Controller', function() {
var controller, $q, $scope, mockService, deferred, $rootScope;
beforeEach(module("controllers"));
beforeEach(function() {
mockService = {
getStuff: function (id) {
deferred = $q.defer();
deferred.resolve({somestuff: 1});
return deferred.promise;
}
};
spyOn(mockService, 'getStuff').and.callThrough();
});
beforeEach(inject(function ($rootScope, $controller, _$q_) {
$scope = $rootScope.$new();
$q = _$q_;
controller = $controller('myController', {
$scope: $scope,
service: mockService
});
}));
it("it should work", function() {
debugger;
});
});
如果我通过Chrome的调试步骤,模拟服务被调用,但在我的控制器中的“onGetStuff”通话不会被调用,任何人都可以找出原因?
谢谢 Mocksy。
thx男人,完全错过了,尽管它在其他职位被提及。 – Mocksy 2015-02-24 09:03:10