代码之家  ›  专栏  ›  技术社区  ›  Aaron Rabinowitz

测试控制器语句未定义

  •  0
  • Aaron Rabinowitz  · 技术社区  · 6 年前

    嗨,我是刚接触karma Jasmine测试的。我试图测试一个函数,但它在我尚未测试的值上抛出了一个错误。这里是错误 TypeError: Cannot set property 'statement' of undefined 下面是我得到错误的描述块切换选项按钮中的相关代码(这是我为注入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', () => {
            //beforeEach(controller.statement = { showOptions: true })
            it('should set the scope value to the index', () => {
                const index = 1;
                const dateId = 1290488400000;
                controller.toggleOptions(index, dateId)
                expect(controller.activeRow).toEqual(index);
            })
    
        })
    
    });
    

    这是控制器中发生错误的代码 this.statement.showOptions 我试着对它进行注释,如果它不在控制器中,效果很好。我试着在construtor和on init中设置它,就像这样 statement: { showOptions: false } 这样地 this.statement.showOptions = true;

    toggleOptions(index, currentDate) {
            // toggle the dropdown with all buttons if its new then showing options is always true
            if (currentDate === this.currentSelectedDate) {
                this.statement.showOptions = !this.statement.showOptions;
            } else {
                this.statement.showOptions = true;
            }
    
            this.activeRow = index;
            this.currentSelectedDate = currentDate;
        }
    

    这可能很简单,因为我才刚刚开始。提前谢谢你。

    2 回复  |  直到 6 年前
        1
  •  1
  •   Estus Flask    6 年前
    beforeEach(controller.statement = { showOptions: true })
    

    不是一种有效的做事方式。

    beforeEach 接受函数。它应该是:

    beforeEach(() => {
      controller.statement = { showOptions: true }
    })
    

    另一个答案解释道, controller 变量未在所用函数的范围内定义。它是在兄弟中定义的 declare . 如果两者都是 describe 使用具有相同本地依赖项的控制器实例 inject(($componentController, ...) 应移动到父级 描述 ,可以与 在每个之前 :

    beforeEach(module('wbbUiApp'));
    
    beforeEach(inject(($componentController, ...));
    
        2
  •  1
  •   kshetline    6 年前

    您已经限制了的变量范围 controller 到第一个descripe()。在第二个descripe()中,控制器将因此而未定义。