1

Hi I am new to testing with karma Jasmine. I was trying to create a new scope with controller.$rootScope.$new(); or even just plain $rootScope.$new(); but I get an error TypeError: Cannot read property '$new' of undefined Here is the relevant code code in the describe block toggle options button I am getting the error (that is the only code I have added besides for injecting rootscope)

fdescribe('Online Statements', () => {
    const module = window.module;
    const inject = window.inject;

    beforeEach(module('wbbUiApp'));

    describe('Controller', () => {
        let scope;
        let controller;
        let injectibles;
        let bindings;

        const createController = userDetails => {
            inject(($componentController,
                $window,
                $filter,
                $rootScope) => {

                // The same `$inject` array values listed in `account-activity.controller.js`
                injectibles = {
                    $window,
                    $filter,
                    $rootScope
                };

                // The same bindings listed in `account-activity.component.js`
                bindings = {
                    accountDetails
                };

                controller = $componentController('onlineStatements', injectibles, bindings);
        });
     };
   describe('toggle options button', () => {
        it('should set the scope value to the index', () => {
            scope = controller.$rootScope.$new(); // problem here
            const index = 1;
            controller.toggleOptions(index)
            expect(scope.activeRow).toEqual(index)
        })

    })

});

this might be something simple as I am just starting. Thank you in advance.

1 Answer 1

1

This should work:

describe('Online Statements', () => {
    beforeEach(module('wbbUiApp'));

    let controller;

    beforeEach(()=>{
        inject(($injector)=> {

        const createController = userDetails => {
            inject(($componentController) => {
                let bindings = {
                    accountDetails
                };

                controller = $componentController('onlineStatements', {}, bindings);
        });
     });
   describe('toggle options button', () => {
        it('should set the scope value to the index', () => {
            controller.$onInit()
            const index = 1;
            controller.toggleOptions(index)
            expect(controller.activeRow).toEqual(index)
        })

    })

});

As u see you do not need to inject anything manually in your case. You should inject $scope only if you have direct dependency on in in component controller. You may want to inject mock for $window sometimes.

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.