测试控制器创建新范围错误

问题描述 投票:1回答:1

嗨,我是新的测试与业力Jasmine。我试图创建一个新的范围与controller.$rootScope.$new();甚至只是简单的$rootScope.$new();但我得到一个错误TypeError: Cannot read property '$new' of undefined这是描述块切换选项按钮中的相关代码代码我得到错误(这是我添加的唯一代码除了注射根镜)

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)
        })

    })

});

这可能是一件简单的事情,因为我刚刚开始。先感谢您。

angularjs ecmascript-6 karma-jasmine
1个回答
1
投票

这应该工作:

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)
        })

    })

});

正如您所见,您不需要在您的情况下手动注入任何东西。只有在组件控制器中直接依赖于in时,才应注入$scope。您可能希望有时为$ window注入模拟。

© www.soinside.com 2019 - 2024. All rights reserved.