angularjs定制服务集成测试
我想对AngularJs应用程序进行集成测试。我想测试实际的服务,而不是模仿它。不知何故,从测试中获取我的服务实例是不起作用的。代码如下:angularjs定制服务集成测试,angularjs,jasmine,Angularjs,Jasmine,我想对AngularJs应用程序进行集成测试。我想测试实际的服务,而不是模仿它。不知何故,从测试中获取我的服务实例是不起作用的。代码如下: var todoApp = angular.module('todoApp', []); todoApp.controller('TodoController', function ($scope, todoService) { $scope.FinalMessage = 'Hello World!'; this.getTodos = functio
var todoApp = angular.module('todoApp', []);
todoApp.controller('TodoController', function ($scope, todoService) {
$scope.FinalMessage = 'Hello World!';
this.getTodos = function() {
$scope.Todos = todoService.getTodos();
};
});
//TODO:将其移动到其文件
todoApp.service(“todoService”,函数(){
测试是:
describe("Integration testing with the Todo service...", function () {
describe("Todo Controller test", function () {
beforeEach(module("todoApp")); // From angular mock not the real module!!
it("Tests the controller returns the message", (inject(function ($rootScope, $controller) {
var $injector = angular.injector(['todoApp']);
var myService = $injector.get('todoService');
//var service = module.service("todoService", todoService);
var scope = $rootScope.newValue();
var controller = $controller("TodoController", { $scope: scope, todoService: myService });
controller.getTodos();
expect(scope.Todos).not.toBe(null);
})));
});
});
不知何故,我无法实例化todoService
谢谢您正在尝试使用注入器实例化服务。TodoService已经是TodoApp模块的一部分,因此ng已经知道如何找到它,并将为您实例化它 试着像这样重写您的规范: (参见plnkr=>上的工作示例)
为了用真实的后端调用测试我的应用程序,我使用了 它的工作原理与Jasmine中的单元测试类似 我将它与Jasmine 2.0一起使用,因此测试如下所示:
it(' myTest', function (done) {
_myService.apiCall()
.then(function () {
expect(true).toBeTruthy();
done()
});
});
注意:由于异步调用,需要done
describe("Integration testing with the Todo service...", function () {
describe("Todo Controller", function () {
var $rootScope;
beforeEach(module("todoApp"));
beforeEach(inject(function(_$rootScope_){
$rootScope = _$rootScope_;
}));
it("Should return the message", (inject(function ($rootScope, $controller, todoService) {
var scope = $rootScope.$new();
var controller = $controller("TodoController", { $scope: scope, todoService: todoService });
controller.getTodos();
expect(scope.Todos).not.toBe(null);
})));
});
});
it(' myTest', function (done) {
_myService.apiCall()
.then(function () {
expect(true).toBeTruthy();
done()
});
});