1

I'm trying to set up Karma unit tests and in my test I want to set a scope variable so I can run the test. I get the error Cannot set property 'expandedSeries' of undefined.

Below is my code. What am I doing wrong?

describe('FormController', function () {
    beforeEach(module('userFormApp'));
    var $controller;
    var $rootScope;

    beforeEach(inject(function (_$controller_, _$rootScope_) {
    $controller = _$controller_;
    $rootScope = _$rootScope_;
    }));

    describe('$scope.getImageSrc', function () {
    var $scope, controller;

    beforeEach(function () {
        $scope = $rootScope.$new();           
        controller = $controller('FormController', { $scope: $scope});
    });

    $scope.expandedSeries = 1;

    it('sets variables ', function () {
        expect($scope).toBeDefined();
        expect($scope.expandedSeries).toBeDefined();
        expect($scope.expandedSeries).toEqual(1);
    });
});

1 Answer 1

2

Instantiate the variable in before each so that the test cases can get it when they start.

describe('$scope.getImageSrc', function () {
var $scope, controller;

beforeEach(function () {
    $scope = $rootScope.$new();           
    controller = $controller('FormController', { $scope: $scope});
$scope.expandedSeries = 1;
});

    it('sets variables ', function () {
    expect($scope).toBeDefined();
    expect($scope.expandedSeries).toBeDefined();
    expect($scope.expandedSeries).toEqual(1);
});

});

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.