我认为通过编写像这样的Jasmine模拟我取得了很好的进展。但我无法解决这个错误。
spyOn方法实际上如何工作?实际上这种方法似乎被广泛使用。在2.0中有些变化
describe('Test Controller', function() {
var $scope, $rootScope, $q, controller, mockService, queryDeferred;
//What is the expected response ?
var expectedResponse = [{ suffix: 'Mr.'}, { prefix: 'Miss'}];
beforeEach(function () {
angular.module("Test", []);
});
beforeEach(module('Test'));
beforeEach(inject(function(_$rootScope_, _$q_, $controller) {
$q = _$q_;
$rootScope = _$rootScope_;
$scope = $rootScope.$new();
mockService = {
query: function() {
queryDeferred = $q.defer();
return queryDeferred.promise;
}
}
spyOn(mockService, 'query').andCallThrough();
// inject the mocked Service into the controller.
controller = $controller('TestController', {
'$scope': $scope,
'ServiceApi': mockService
});
}));
});
TypeError: undefined is not a function
at Object.<anonymous> (d:/karma-requirejs-Angular/test/PromiseSpec.j
s:34:39)
at Object.invoke (d:/karma-requirejs-Angular/lib/angular/angular.js:
4152:17)
at Object.workFn (d:/karma-requirejs-Angular/lib/angular-mocks/angul
ar-mocks.js:2255:20)
Error: Declaration Location
at window.inject.angular.mock.inject (d:/karma-requirejs-Angular/lib
/angular-mocks/angular-mocks.js:2226:25)
at Suite.<anonymous> (d:/karma-requirejs-Angular/test/PromiseSpec.js
:20:16)
at d:/karma-requirejs-Angular/test/PromiseSpec.js:7:3
at Object.context.execCb (d:/karma-requirejs-Angular/node_modules/re
quirejs/require.js:1658:33)
at Object.Module.check (d:/karma-requirejs-Angular/node_modules/requ
更新:
我发布这个工作代码作为答案。我解决了一些问题。
describe('Test Controller', function() {
var $q,
$rootScope,
$scope,
mockService,
mockResponse = [{ suffix: 'Mr.'}, { prefix: 'Miss'}];
beforeEach(module('myApp'));
beforeEach(inject(function(_$q_,
_$rootScope_) {
$q = _$q_;
$rootScope = _$rootScope_;
}));
beforeEach(inject(function($controller) {
$scope = $rootScope.$new();
mockService = {
query: function() {
queryDeferred = $q.defer();
return {$promise: queryDeferred.promise};
}
}
spyOn(mockService, 'query').and.callThrough();
$controller('TestCtrl', {
'$scope': $scope,
'testService': mockService
});
}));
describe('Given a setup for the invocation of the mock query', function() {
beforeEach(function() {
queryDeferred.resolve(mockResponse);
$rootScope.$apply();
});
it('When the call to the mock service is issued ', function() {
resolvespy = jasmine.createSpy('resolve');
rejectspy = jasmine.createSpy('reject');
expect(mockService.query).toHaveBeenCalled();
var q = $scope.getPromise();
q.then(resolvespy,rejectspy);
queryDeferred.resolve(mockResponse);
$rootScope.$apply();
expect(resolvespy).toHaveBeenCalledWith(mockResponse);
expect(rejectspy).not.toHaveBeenCalled();
});
it('Then the mock service should have returned the correct response', function() {
expect($scope.test).toBe(mockResponse);
});
it('Then the mock service should have returned the correct response', function() {
expect($scope.testfunction).toBeDefined();
});
it("should do something else when something happens", function() {
$scope.testfunction();
expect($scope.value).toBe('value');
});
});
});
答案 0 :(得分:19)
用Jasmine 2.0你应该替换:
andCallThrough();
使用:
and.callThrough();