我有以下组件:
angular.module('foo')
.component('searchInput', {
bindings: {
text: "<query"
},
templateUrl: 'components/searchInput/searchInput.html',
controller: 'SearchInputCtrl'
});
以下内容通过:
expect(component.text).toBe('bar');
我必须使用以下代码:
var component = $componentController('searchInput',
{$scope: {}},
{
text: 'bar'
}
);
但是,我想测试绑定到"text"的值是否来源于"query"。此不起作用:
var component = $componentController('searchInput',
{$scope: {}},
{
query: 'bar'
}
);
您可以通过编译组件来测试这类事情。例如
inject(function($compile, $rootScope) {
var parentScope = $rootScope.$new();
parentScope.myVar = 'test';
var element = angular.element('<search-input query="myVar"></search-input>');
var compiledElement = $compile(element)(parentScope);
parentScope.$digest();
var scope = compiledElement.isolateScope();
expect(scope.$ctrl.text).toBe('test');
});