要测试angular 1.5组件,文档建议您使用ngMock的$ componentController而不是使用$ compile,如果您不需要测试任何DOM。
但是,我的组件使用ngModel,我需要将其传递到$ {1}} for $ componentController,但是没有办法以编程方式获取ngModelController;测试它的唯一方法是实际$在其上编译一个元素,因为这个问题仍然是开放的:https://github.com/angular/angular.js/issues/7720。
有没有办法测试我的组件控制器而不需要使用$编译它?我也不想自己模仿ngModelController,因为它的行为有点广泛,如果我的测试依赖于假的而不是真实的东西,那么新版本的Angular可能会破坏它(尽管可能不是'给定Angular 1的问题正逐步消失。)
答案 0 :(得分:3)
tl; dr:解决方案在第三个代码块中。
但无法以编程方式获取ngModelController
不是那种态度。 ;)
你可以通过编程方式获得它,只是一个小环形交叉口。这样做的方法是in the code for ngMock
's $componentController
service(在此解释);使用$injector.get('ngModelDirective')
进行查找,控制器函数将作为controller
属性附加到它:
this.$get = ['$controller','$injector', '$rootScope', function($controller, $injector, $rootScope) {
return function $componentController(componentName, locals, bindings, ident) {
// get all directives associated to the component name
var directives = $injector.get(componentName + 'Directive');
// look for those directives that are components
var candidateDirectives = directives.filter(function(directiveInfo) {
// ...
});
// ...
// get the info of the component
var directiveInfo = candidateDirectives[0];
// create a scope if needed
locals = locals || {};
locals.$scope = locals.$scope || $rootScope.$new(true);
return $controller(directiveInfo.controller, locals, bindings, ident || directiveInfo.controllerAs);
};
}];
虽然在实例化时需要为$element
和$attrs
提供ngModelController本地化。 The test spec for ngModel
demonstrates exactly how to do this in its beforeEach
call:
beforeEach(inject(function($rootScope, $controller) {
var attrs = {name: 'testAlias', ngModel: 'value'};
parentFormCtrl = {
$$setPending: jasmine.createSpy('$$setPending'),
$setValidity: jasmine.createSpy('$setValidity'),
$setDirty: jasmine.createSpy('$setDirty'),
$$clearControlValidity: noop
};
element = jqLite('<form><input></form>');
scope = $rootScope;
ngModelAccessor = jasmine.createSpy('ngModel accessor');
ctrl = $controller(NgModelController, {
$scope: scope,
$element: element.find('input'),
$attrs: attrs
});
//Assign the mocked parentFormCtrl to the model controller
ctrl.$$parentForm = parentFormCtrl;
}));
因此,根据我们的需要调整,我们得到这样的规范:
describe('Unit: myComponent', function () {
var $componentController,
$controller,
$injector,
$rootScope;
beforeEach(inject(function (_$componentController_, _$controller_, _$injector_, _$rootScope_) {
$componentController = _$componentController_;
$controller = _$controller_;
$injector = _$injector_;
$rootScope = _$rootScope_;
}));
it('should update its ngModel value accordingly', function () {
var ngModelController,
locals
ngModelInstance,
$ctrl;
locals = {
$scope: $rootScope.$new(),
//think this could be any element, honestly, but matching the component looks better
$element: angular.element('<my-component></my-component>'),
//the value of $attrs.ngModel is exactly what you'd put for ng-model in a template
$attrs: { ngModel: 'value' }
};
locals.$scope.value = null; //this is what'd get passed to ng-model in templates
ngModelController = $injector.get('ngModelDirective')[0].controller;
ngModelInstance = $controller(ngModelController, locals);
$ctrl = $componentController('myComponent', null, { ngModel: ngModelInstance });
$ctrl.doAThingToUpdateTheModel();
scope.$digest();
//Check against both the scope value and the $modelValue, use toBe and toEqual as needed.
expect(ngModelInstance.$modelValue).toBe('some expected value goes here');
expect(locals.$scope.value).toBe('some expected value goes here');
});
});
ADDENDUM:您也可以通过在ngModelDirective
中注入beforeEach
并在describe
块中设置var以包含控制器功能来进一步简化它,就像您使用的服务一样$controller
。
describe('...', function () {
var ngModelController;
beforeEach(inject(function(_ngModelDirective_) {
ngModelController = _ngModelDirective_[0].controller;
}));
});