I have an AngularJS application that I’m trying my hand at unit tests, although I’m stuck at my first hurdle.
I keep getting the following error TypeError: undefined is not an object (evaluating 'that.options.length')
when running the below controller and unit test.
I dont see where the undefined is and any help would be appreciated.
controller
function dropdownController() {
const that = this;
let selected = '';
that.$onInit = () => {
if (that.options.length) {
selected = that.options.includes(that.default) ? that.default : that.options[0];
}
};
}
unit test
describe('Dropdown component', () => {
// load the controller's module
beforeEach(() => {
angular.mock.module('app');
});
var $componentController, scope, component;
// Initialize the controller and a mock scope
beforeEach(angular.mock.inject(function ($rootScope, _$componentController_) {
scope = $rootScope.$new();
$componentController = _$componentController_;
component = $componentController('dropdown', {$scope: scope});
// component.$onInit();
}));
it('should set dropdown options on init', function () {
let that = this;
that.options = ['Tom', 'Dick', 'Harry'];
component.$onInit();
expect(that.options.length).toEqual(3);
});
});
Source: AngularJS
from Angular Questions https://angularquestions.com/2017/10/11/karma-unit-test-failing-typeerror-undefined-is-not-an-object-evaluating-that-options-length/
via @lzomedia #developer #freelance #web #lzomedia.com
No comments:
Post a Comment