我对angular / jasmine / karma非常新,而且我在为我的控制器编写测试时遇到了麻烦。测试本身运行成功,但在测试运行中我收到以下错误:
Error: userFactory() method does not exist
userFactory()是在我的控制器中对返回promise的服务进行的方法调用。我不确定如何确保在测试中正确定义。
这是我的代码:
app.js
(function () {
angular.module('mdotTamcCouncil', ['mdotTamcCouncil.core', 'blurb']);
angular.module('mdotTamcCouncil.core', []);
})();
导语-service.js
(function () {
angular.module('mdotTamcCouncil.core').factory('blurbsFactory', function ($http) {
var promise = null;
return function () {
if (promise) {
// If we've already asked for this data once,
// return the promise that already exists.
return promise;
} else {
promise = $http.get(jsGlobals.blurbsDataURL);
return promise;
}
};
});
})();
用户service.js
(function () {
angular.module('mdotTamcCouncil.core').factory('userFactory', function ($http) {
var promise = null;
return function () {
if (promise) {
// If we've already asked for this data once,
// return the promise that already exists.
return promise;
} else {
promise = $http.get(jsGlobals.userDataURL);
return promise;
}
};
});
})();
导语-controller.js
(function () {
angular.module('blurb')
.controller('BlurbController', ['$scope', 'blurbsFactory', 'userFactory', function ($scope, blurbsFactory, userFactory) {
$scope.content = "";
$scope.blurbs = {};
$scope.currentUser = {};
this.editMode = false;
userFactory().success(function (data) {
$scope.currentUser = data;
});
blurbsFactory().success(function (data) {
$scope.blurbs = data;
$scope.content = $scope.blurbs[$scope.textKey];
});
this.enterEditMode = function () {
this.editMode = true;
};
this.saveEdits = function () {
this.editMode = false;
$scope.blurbs[$scope.textKey] = $scope.content;
};
}]);
})();
导语-module.js
(function () {
'use strict';
angular.module('blurb', ['ngSanitize', 'mdotTamcCouncil.core']);
})();
和我的测试规范:
describe('BlurbController', function () {
var scope, controllerService;
beforeEach(module('mdotTamcCouncil'));
beforeEach(module('mdotTamcCouncil.core'));
beforeEach(module('blurb'));
beforeEach(inject(function ($rootScope, $controller) {
scope = $rootScope.$new();
controllerService = $controller;
}));
it("should get 'user' from 'data/user.json'", inject(function ($httpBackend) {
$httpBackend.expectGET("data/user.json").respond({"userName": "myera","email": "something@something.com","isAdmin": true});
$httpBackend.expectGET("data/blurbs.json").respond('{"mainPageIntro": "<h2>Welcome</h2>"}');
ctrl = controllerService('BlurbController', { $scope: scope });
$httpBackend.flush();
expect(scope.currentUser).toEqual({"userName": "myera","email": "something@something.com","isAdmin": true});
expect(scope.blurbs).toEqual({ "mainPageIntro": "<h2>Welcome</h2>" });
}));
});
我通过阅读博客和stackoverflow答案将这些拼凑在一起。我不确定我是否正确地做到了。
测试本身实际上已通过,但我在控制台中收到有关缺少&#34; userFactory()&#34;的错误。方法。我假设我也会收到&#34; blurbFactory()&#34;的消息。方法,如果它走得那么远。我不相信我可以在没有先解决这些错误的情况下测试控制器的实际功能。
我做错了什么?非常感谢您提供的任何帮助。
答案 0 :(得分:0)
我相信您所看到的问题是因为您没有包含“mdotTamcCouncil.core”和“#39; mdotTamcCouncil.core&#39;模块作为blurb模块中的依赖项。定义它时,您应该能够使用
传入依赖项列表angular.module('blurb', ['mdotTamcCouncil.core']);
您只收到一个控制台错误,因为当userFactory()失败时执行会停止。我不确定为什么测试显示为通过,它应该选择异常并且失败 - 对于您选择的测试运行员来说可能是个奇怪的。