Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing 单元测试AngularJS路由抛出错误:意外请求_Unit Testing_Angularjs_Karma Runner - Fatal编程技术网

Unit testing 单元测试AngularJS路由抛出错误:意外请求

Unit testing 单元测试AngularJS路由抛出错误:意外请求,unit-testing,angularjs,karma-runner,Unit Testing,Angularjs,Karma Runner,下面是我的Jasmine RouteSpec.js describe("Todo Routes", function(){ var route; var rootScope; var location; beforeEach(function(){ module('todoApp'); inject(function($route, $location, $rootScope){ route = $rou

下面是我的Jasmine RouteSpec.js

describe("Todo Routes", function(){
    var route;
    var rootScope;
    var location;

    beforeEach(function(){
        module('todoApp');

        inject(function($route, $location, $rootScope){
            route = $route;
            location = $location;
            rootScope = $rootScope;
        }); 
    });

    it("should navigate to todo list", function(){
        expect(route.current).toBeUndefined();
        location.path('/todos');
        rootScope.$digest();
        expect(route.current.templateUrl).toBe('app/html/listTodos.html');
    });
});
下面是我的app.js

var todoModule = angular.module("todoApp", []);

todoModule.config(function($routeProvider){
    $routeProvider.when('/todos', {
        templateUrl: '../html/listTodos.html',
        controller: 'TodoListController'
    })
    .otherwise({redirectTo: '/todos'});
});

todoModule.controller("TodoListController", function($scope, $log){
    $scope.todos = [{title: "My first task", done: false}];
    $log.log('In list controller');
});
执行此规范会引发以下错误:

错误:意外请求:GET../html/listtoos.html 不需要更多的请求 错误() 在$httpBackend(C:/Learn/Javascript/todo_app/libs/angular mocks.js:934:9) 在sendReq(C:/Learn/Javascript/todo_app/libs/angular.js:9146:9) 在$http(C:/Learn/Javascript/todo_app/libs/angular.js:8937:17) at Function.$http.(匿名函数)(C:/Learn/Javascript/todo_app/libs/angular.js:9080:18) 在$q.when.then.then.next.locals(C:/Learn/Javascript/todo_app/libs/angular.js:7440:34) 在wrappedCallback(C:/Learn/Javascript/todo_app/libs/angular.js:6846:59) 在wrappedCallback(C:/Learn/Javascript/todo_app/libs/angular.js:6846:59) 在C:/Learn/Javascript/todo_app/libs/angular.js:6883:26 在Object.Scope.$eval(C:/Learn/Javascript/todo_app/libs/angular.js:8057:28)


这意味着有一个AJAX调用用于获取模板。 $httpBackend.expectGET('app/html/listtoos.html')。可以在调用path()之前放置respond(200):


在测试中将root设置为/todos会触发http加载与此路由关联的部分。但是,由于您没有模拟httpBackend来告诉它在收到此部分的请求时要做什么,因此您会出现此错误。这是否意味着Juho提供的解决方案也无法工作?不知道。不是一个有棱角的专家。我只是在分析你的错误。
describe("Todo Routes", function(){
    var route;
    var rootScope;
    var location;
    var httpBackend;

    beforeEach(function(){
        module('todoApp');

        inject(function($route, $location, $rootScope, $httpBackend){
            route = $route;
            location = $location;
            rootScope = $rootScope;
            httpBackend = $httpBackend;
        }); 
    });

    it("should navigate to todo list", function(){
        httpBackend.expectGET('app/html/listTodos.html').respond(200);//mimicking the AJAX call
        expect(route.current).toBeUndefined();
        location.path('/todos');
        rootScope.$digest();
        expect(route.current.templateUrl).toBe('app/html/listTodos.html');
    });
});