javascript - Karma-Jasmine:期望undefined被定义+Controller+Service

标签 javascript angularjs karma-jasmine

我正在尝试在 Controller 上运行单元测试,但我遇到了以下错误:

Expected undefined to be defined.

我知道什么是未定义的,但我不知道为什么它是未定义的以及如何解决它。 让我粘贴我的代码以便更好地理解。

Controller

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) 触发错误。此处,controlLabels 未定义。是因为值来自 textService 吗?
  • 但是为什么它是未定义的以及如何解决它。
  • 我们将不胜感激任何帮助。

最佳答案

您没有在 beforeEach 中将模拟 textService 注入(inject)到 LicensingController 中。添加它,它应该开始工作。

beforeEach(inject(function (_$controller_, _textService_) {    
    LicensingController = _$controller_("LicensingController", { textService: _textService_ });
}));

您还需要删除第二个 beforeEach 或至少在其中提供 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",
            }
          }
      };
    });
}));

关于javascript - Karma-Jasmine:期望undefined被定义+Controller+Service,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49784248/

相关文章:

javascript - 将函数应用于对象树中属性的所有实例

javascript - $scope.$watch inside 指令在它应该的时候没有被调用

unit-testing - 解释 karma 单元测试时间

angularjs - 错误 : [$injector:unpr] Unknown provider: $scopeProvider <- $scope Error

javascript - 获得平滑且有点延迟的滚动

javascript - 禁止使用 ExtJS 将列拖动到某些位置

javascript - 无法获取 401 响应正文

php - php 中的 Angular.js 模式窗口回显不起作用

javascript - 如何从AngularJS中的另一个服务方法调用工厂方法?

javascript - 期望返回值等于 $promise