1

以下は私のJasmine RoutesSpec.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');
});

この仕様を実行すると、次のエラーがスローされます。

Error: Unexpected request: GET ../html/listTodos.html No more request expected at Error () at $httpBackend (C:/Learn/Javascript/todo_app/libs/angular-mocks.js:934:9) at sendReq ( C:/Learn/Javascript/todo_app/libs/angular.js:9146:9) at $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 at Object.Scope.$eval (C:/Learn/Javascript/todo_app/libs/angular.js:8057:28)

4

1 に答える 1

2

これは、テンプレートを取得するための AJAX 呼び出しがあることを意味します。$httpBackend.expectGET('app/html/listTodos.html').respond(200) は、path() を呼び出す前に配置できます。

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');
    });
});
于 2014-03-20T09:20:37.607 に答える