我一直在编写角度指令的测试用例。我在测试中注入我的控制器,我能够存储$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
设置默认测试值或模拟它。
如何从测试中存根/模拟步骤的函数和变量?
有什么建议吗?