Reputation: 1473
I am trying to run a Unit Test on a Controller, but I am facing the following Error:
Expected undefined to be defined.
I know what is undefined, but I don't know why it is undefined and how to fix that. Let me paste my code for better understanding.
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 Text Service basically just contain Object for Strings and returns an Object.
(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",
}
}
};
}
})();
Unit Test
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();
});
});
});
Upvotes: 1
Views: 1822
Reputation: 2858
You are not injecting a mock textService
into LicensingController
in your beforeEach
. Add that and it should start working.
beforeEach(inject(function (_$controller_, _textService_) {
LicensingController = _$controller_("LicensingController", { textService: _textService_ });
}));
You also need to remove the second beforeEach
or at least provide a mock implementation of textService
in there.
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",
}
}
};
});
}));
Upvotes: 1