使用 Jasmine 时出现未定义函数错误
Undefined Function Error when using Jasmine
我认为通过编写这样的 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');
});
});
});
在 Jasmine 2.0 中,您应该替换:
andCallThrough();
与:
and.callThrough();
我认为通过编写这样的 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');
});
});
});
在 Jasmine 2.0 中,您应该替换:
andCallThrough();
与:
and.callThrough();