类型错误单元测试茉莉

TypeError Unit Test Jasmine

本文关键字:单元测试 错误 类型      更新时间:2023-09-26

我需要一些帮助,我在实际阅读整个文件方面取得了一些进展,但是,我遇到了一个相当大的障碍(对我来说这是体面的,我是新手)。我试图模拟大部分控制器,当我读到文件的一半时,我遇到了一个问题,我不知道如何克服它。我将在下面尽可能多地包含代码。

LocationController.js

angular
.module('TDE')
.controller('LocationController', ['$rootScope', '$scope', '$location', '$window', '$document', 'LocationService', 'HeaderFooterService', 'SearchService', 'TranslationService', 'MTDE_CONFIG', 'LocationPartnerAssignmentService', 'ExperimentService', function ($rootScope, $scope, $location, $window, $document, $LocationService, $HeaderFooterService, $SearchService, $TranslationService, $MTDE_CONFIG, $LocationPartnerAssignmentService, $ExperimentService) {
//passes over this code
$scope.init = function () {
    // load dropdown for crop list
    // load dropdown for business partner list
    //load dropdown for experimenttype
    //load dropdown for IrrigationType
    //load dropdown for previousCrop
    var locationId = $SearchService.GetLocationId();
    $scope.LocationId=locationId;
    var experimentId = $SearchService.GetExperimentId();
    var experimentPartnerAssignmentId = $SearchService.GetExperimentPartnerAssignmentId();
    //load nitrogen value
    $ExperimentService.GetCrop(onSuccessCropCode,cropId);
    // get location detail from database
    if (locationId === "00000000-0000-0000-0000-000000000000") {
        // Load the Unassigned Location here
        $scope.IsAssinged = false;
    }
    else {
        $LocationService.Detail(onSuccessDetail, locationId);
    }
    // get growing year list
    $scope.GrowingYearList = $LocationService.GrowingYearList();
}
$scope.init(); //I NEED TO BE MOCKED SOMEHOW

Helper.js

ddescribe("Phases of Testing: The Journey", function () {
describe("Phase I: Test that Jasmine runs", function () {
    it("should test Jasmine is up", function () {
        expect(true).toBe(true);
    });
});
describe("Phase II: Try something", function () {
    var DBMock, SyncMock, baseMock, configMock, TransMock, loginMock, locMock, headerMock,
            searchMock, LPAMock, expMock, mockScope, $location, $scope, ctrl, initMock;
    beforeEach(function () {
        angular.mock.module('TDE');
        inject(function (_$location_, _$rootScope_, _$controller_) {
            $location = _$location_;
            $scope = _$rootScope_.$new();
            ctrl = _$controller_('LocationController', {
                '$scope': $scope
            });
        });
    });
    it("should be able to grab something", function () {
        expect(true).toBe(true);
        expect($scope).toBeDefined();
        expect($scope.PlantingDateNull()).toBeTruthy();
    });
  });
});

编辑:现在问题归结为在初始定义$scope.init()函数调用之后模拟它。

:

var myApp = angular.module('myApp', []);
myApp.controller('MyCtrl', function ($scope, environment) {
    $scope.testValue = 'test environment';
    $scope.init = function() {
        $scope.testValue = 'development environment';
    };
    environment.development && $scope.init();
});
myApp.factory('environment', function() {
    return {
        development: true
    }
});

最重要的一行是environment.development && $scope.init();

environment.development:

  • 如果求值为TRUE - $scope.inint()将被称为
  • 求值为FALSE -将阻止调用$scope.inint()

为了证明这一点,我将用两个值来存根这个服务

测试:

beforeEach(function () {
    module('myApp');
});
beforeEach(function () {
    module(function ($provide) {
        $provide.value('environment', mockEnvironment);
    });
});
describe('MyCtrl', function () {
    beforeEach(inject(function ($rootScope, $controller) {
        scope = $rootScope.$new();
        controllerInstantiate = $controller;
    }));
    it('prevents call .init() on test environment', function () {
        mockEnvironment.development = false;
        createController();
        expect(scope.testValue).toBe('test environment');
    });
    it('calls init on development environment', function () {
        mockEnvironment.development = true;
        createController();
        expect(scope.testValue).toBe('development environment');
    });
    function createController() {
        return controllerInstantiate('MyCtrl', {
            '$scope': scope
        });
    }
});
http://jsfiddle.net/krzysztof_safjanowski/2LUp3/

您必须为$LocationService而不是LocationService提供mock,并为strip方法创建spy,如:

describe('myApp', function () {
var scope,
controller,
$mockLocationService = {};
beforeEach(function () {
    module('myApp');
});
beforeEach(function () {
    module(function ($provide) {
        $provide.value('$LocationService', $mockLocationService);
    });
});
describe('MyCtrl', function () {
    beforeEach(function () {
        $mockLocationService.Strips = jasmine.createSpy('$LocationService.Strips');
    });
    beforeEach(inject(function ($rootScope, $controller) {
        scope = $rootScope.$new();
        controller = $controller('MyCtrl', {
            '$scope': scope
        });
    }));
    it('calls Strips method', function () {
        expect($mockLocationService.Strips).toHaveBeenCalled();
    });
});

});

答案不再与编辑的问题相关,对不起。