Javascript Karma Jasmine:预期未定义将被定义+;控制器&x2B;服务

Javascript Karma Jasmine:预期未定义将被定义+;控制器&x2B;服务,javascript,angularjs,karma-jasmine,Javascript,Angularjs,Karma Jasmine,我试图在控制器上运行单元测试,但遇到以下错误: Expected undefined to be defined. 我知道什么是未定义的,但我不知道为什么它是未定义的,以及如何修复它。 让我粘贴代码以便更好地理解 控制器 angular .module("app.licensing", []) .controller("LicensingController", LicensingController) LicensingController.$inject = ["

我试图在控制器上运行单元测试,但遇到以下错误:

Expected undefined to be defined.
我知道什么是未定义的,但我不知道为什么它是未定义的,以及如何修复它。 让我粘贴代码以便更好地理解

控制器

angular
    .module("app.licensing", [])
    .controller("LicensingController", LicensingController)

    LicensingController.$inject = ["textService"];

    function LicensingController(textService) {
        var vm = this;

        vm.licForm = {
            accountName: null,
            ticketNo: null
        };

        vm.controlLabels = textService.licensing.controlLabels;
    }
textService文本服务基本上只包含字符串的对象并返回一个对象

(function () {
    "use strict";

    angular
        .module('app')
        .factory("textService", textService)

    function textService() {
        return {

            //Object for Licensing Module
            licensing: {
                pageTitle: "Page Title",
                controlLabels: {
                    accountName: "Account Name",
                    ticketNo: "Ticket No",
                    hostId: "Host Id",
                }
            }
        };
    }
})();
单元测试

describe("-----> LicensingController", function () {

    var LicensingController;
    var textService;

    beforeEach(angular.mock.module("app.licensing"));

    beforeEach(function () {

        module(function ($provide) {
            $provide.value("textService", textService);
        });
    });

    beforeEach(inject(function (_$controller_) {

        LicensingController = _$controller_("LicensingController", {
        });
    }));

    describe("-----> Licensing Form", function () {

        it("--> License Controller should be Defined.", function () {
            expect(LicensingController).toBeDefined();
        });

        it("--> 'licForm' Object must be Defined.", function () {
            expect(LicensingController.licForm).toBeDefined();
        });

        it("--> 'controlLabels' Object must be Defined.", function () {
            expect(LicensingController.controlLabels).toBeDefined();
        });
    });
});
  • 在单元测试中,第三个测试(controlLabels)触发错误。这里,controllabel是未定义的。是否因为该值来自textService
  • 但是为什么它是未定义的,以及如何解决它
  • 任何帮助都将不胜感激

您没有在每次之前将模拟
文本服务
注入
中的
许可控制器
。再加上这个,它就会开始工作了

beforeEach(inject(function (_$controller_, _textService_) {    
    LicensingController = _$controller_("LicensingController", { textService: _textService_ });
}));
您还需要在每个
之前删除第二个
,或者至少在其中提供一个
textService
的模拟实现

beforeEach(angular.mock.module(function($provide) { 
    $provide.service('textService', function () {
        return {

          //Object for Licensing Module
          licensing: {
            pageTitle: "Page Title",
            controlLabels: {
              accountName: "Account Name",
              ticketNo: "Ticket No",
              hostId: "Host Id",
            }
          }
      };
    });
}));

我试着以各种方式注入服务。还试着创造间谍北京。但是什么都没用(好的。我用一个更好的注入模拟服务的例子更新了我的答案。如果我粘贴代码“$provide.service('textService')…”,那么无论您是否在控制器中注入textService,单元测试都是有效的。老实说,我不知道发生了什么,但测试是有效的。只有一个问题:如果我的“textService”有200行代码,我还需要在单元测试中包含所有200行代码吗?@AnkitPrajapati,这取决于你-你可以测试你的控制器,或者你可以测试你的控制器+你的服务。如果服务重定向调用到$http-它值得模拟。如果服务只存储一些文本常量-真的没有理由模拟我t、 只需将模块与您的服务一起添加到test.beforeach(angular.mock.module(“app”));