Angular测试用例模拟控制器作为值

时间:2017-06-06 05:37:44

标签: angularjs unit-testing mocha testcase

我一直在编写角度指令的测试用例。我在测试中注入我的控制器,我能够存储$scope值,但我无法存储控制器的this值,这给了我未定义和测试用例不通过。我有以下角度控制器:

export default class myCtrl {
    constructor($rootScope, $ngRedux, $scope) {
        const unsubscribe = $ngRedux.connect(this.mapStateToThis, bodyActions)(this);
        $scope.$on('$destroy', unsubscribe);
        const step = this;

        step.buttonClassName = 'sBtn';
        step.appState.selectedService = step.appState.selectedService || '';
        console.log(step.customFunction());

    }
    customFunction() {
        return 'test';
    }
     mapStateToThis(state) {
        return {
            appState: state.app
        };
    }
}

这是以下测试用例代码:

describe('directive test case', function () {
    var $scope,
        $controller,
        compile,
        directiveElem,
        innerScope,
        element,
        compiledElement,
        state, step;

    beforeEach(inject(function ($compile, $rootScope, $controller, $state, _$httpBackend_, $q) {    inject arguments of given function
        $httpBackend = _$httpBackend_
        state = $state
        compile = $compile
        $scope = $rootScope.$new();
        $scope.test = {};
        $httpBackend.when('GET', 'languages/locale-en.json').respond(languageEng);
        console.log(myCtrl);
        step = $controller(myCtrl, {$scope: $scope, test: 'test'});  // Problem is here
    }
}

正如您所看到的,我正在使用controller as语法。因此step具有this值。我无法为step.appState设置默认测试值或模拟它。

如何从测试中存根/模拟步骤的函数和变量?

有什么建议吗?

0 个答案:

没有答案