繁体   English   中英

TypeError单元测试茉莉花

[英]TypeError Unit Test Jasmine

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

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()函数调用。 在任何地方,我都找不到很好的帮助来源。

您必须为$ LocationService而不是LocationService提供模拟,并为Strips方法创建间谍,如下所示:

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();
    });
});

});

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

应用:

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()

为了证明这一点,我将使用两个值对这个Servis进行存根

测试:

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/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM