I'm working on an angular app which was scaffolded using Yeoman. The generator comes with Karma/Jasmine configured and a test file on the main controller. I have over written the code in the main controller with code needed for my app. Now I'm trying to write tests for my code using the test file that was generated, and I'm running into issues.
Here is my main.controller.js (focusing on one function)
(function () {
"use strict";
angular
.module("myApp")
.controller("MainController", function($log) {
var vm = this;
function calculateWorkspaceTabsWidth() {
vm.workspaceWidth = {
"width": 70 / vm.workspaces.length + "%"
};
});
})();
Here is my main.controller.spec.js (written only for the function above)
(function () {
"use strict";
describe('MainController', function () {
var vm;
beforeEach(module('myApp'));
beforeEach(function (_$controller_) {
vm = _$controller_('MainController');
vm.workspaces = ["One", "Two", "Three", "Four", "Five"];
});
it('should be a percentage', function () {
vm.calculateWorkspaceTabsWidth();
expect(vm.workspaceStyle.width).toEqual('14%');
});
});
})();
This is the error message I recieve when I run this test.
TypeError: 'undefined' is not an object (evaluating 'vm.calculateWorkspaceTabsWidth')
What am I doing wrong?
Also, how would I write the test case differently if this function was on the scope? (ie: vm.calculateWorkspaceTabsWidth = function () {})
Thanks in advance for the help.