使用$ window.location.pathname对angularjs进行单元测试

时间:2016-08-16 09:03:12

标签: angularjs unit-testing

我有一个像这样的angularjs控制器:

myApp.controller('SetCourseCtrl', function($scope, $http, $window,$sce) {
var path = $window.location.pathname;
path = path.split('/');
var courseID = path.pop();
$http.post('/course/getCourse', {
    courseID: courseID
}).then(function(result) {
    var trainerJSON = result.data.courseTrainer;
    var trainers = [];
    for (var i = 0; i < trainerJSON.length; i++) {
        trainers.push(trainerJSON[i].text);
    }
    $scope.courseName = result.data.courseName;
    $scope.courseTrainer = trainers;
    $scope.courseTrainerPage = result.data.courseTrainerPage;
    $scope.courseDescription = $sce.trustAsHtml(result.data.courseDescription);
    $scope.courseCategory = result.data.courseCategory;
    $scope.courseDocuments = result.data.courseDocuments;
});

});

我为业力写了一个测试用例:

  describe("SetCourseCtrl Unit testing #5", function() {
    var controller, $controller, $scope, $rootScope, createController, $window;
    beforeEach(angular.mock.module("myApp"));
    beforeEach(inject(function($injector) {
        $rootScope = $injector.get('$rootScope');
        $controller = $injector.get('$controller');
        $httpBackend = $injector.get('$httpBackend');
        createController = {
            setCourse: function() {
                return $controller('SetCourseCtrl', {
                    $scope: $rootScope
                });
            }
        }
    }));

    it('Test 2: post /course/getCourse', inject(function($controller) {
        controller = createController.setCourse();
        var path = $window.location.pathname;
        path = path.split('/');
        var courseID = path.pop();
        var result = {
          data: {
            courseName: 'agile',
            courseTrainerPage: 'trainer page',
            courseDescription: 'description',
            courseCategory: 'category',
            courseDocuments: 'doc.pdf'
          }
        };
        $httpBackend.whenPOST('/course/getCourse', {
            courseID: courseID
        }).respond(result);
        $httpBackend.flush();
        expect($rootScope.courseName).toBe(result.data.courseName);
        expect($rootScope.courseTrainerPage).toBe(result.data.courseTrainerPage);
        expect($rootScope.courseDescription).toBe(result.data.courseDescription);
        expect($rootScope.courseCategory).toBe(result.data.courseCategory);
        expect($rootScope.courseDocuments).toBe(result.data.courseDocuments);

    }));

});

但是当我运行此测试时,会发生错误:

  

PhantomJS 2.1.1(Windows 8 0.0.0)SetCourseCtrl单元测试#5测试   2:post / course / getCourse FAILED           错误:意外请求:POST / course / getCourse

有人可以帮我解释一下吗?我认为这是因为$window.location.pathname,我不能嘲笑它,但我不确定。

1 个答案:

答案 0 :(得分:0)

错误消息表明,对/course/getCourse的POST请求是意外的。

您需要插入这样的期望行:

$httpBackend.whenPOST('/course/getCourse', {
    courseID: courseID
}).respond(result);
$httpBackend.expectPOST('/course/getCourse');
$httpBackend.flush();