Следующий код в $ timeout никогда не вызывается. Я могу поставить любой неправильный тест мне нравится там и тест всегда проходит (есть подобный вопрос (Karma e2e testing: how to know when the DOM is ready?), но он не обеспечивает решения):Karma angularjs тестирование: тестовый код в тайм-ауте никогда не выполняется
it('should display a filter row when attribute sg-live-filtering is present', function()
{
angular.mock.inject(function($compile, $rootScope, $timeout) {
var elem = $compile('<div sg-grid sg-data="api/grid/accounts" sg-live-filtering></div>')(scope); // the angular-kendo grid
$rootScope.$apply();
var table = elem.find('table[role="grid"]'); // find the kendo grid
expect(table.length).toBe(1);
var header = table.find('thead'); // find the grid's table header
expect(header.length).toBe(1);
$timeout(function() {
// get the second row in the header and check it has a specific class
expect(header.find('tr').eq(1).hasClass('sg-grid-filter-row')).toBeTruthy();
// e.g. I could do this and it would still pass!!!
expect(header.find('tr').eq(500));
});
}
}
PhantomJS 1.9.2 (Windows 7): Выполненная 1 из 871 (пропущено 383) УСПЕХ (0 секунд/0
Это то, что он выглядит в браузере:
кендо сетка создается с помощью STANDAR d angularjs директива:
angular.module('sgComponents').directive('sgGrid', [
templateUrl: 'sg-grid.html',
// lots of kendo code follows to build the grid
]);
Внешний шаблон SG-grid.html:
<div sg-grid sg-data="api/grid/accounts"
sg-live-filtering> <!-- the attribute I want to test -->
</div>
При выполнении кода директивы, есть проверка, чтобы увидеть, если SG-живой фильтрации атр присутствует. Если да, то утилита функция вызывается добавить строку, которую вы видите выделены в изображении таблицы заголовок сетки в:
if (attrs.sgLiveFiltering) {
/*
timeout is needed to ensure DOM is ready. COULD THIS BE THE PROBLEM?
*/
$timeout(function() {
/*
this function adds the filter row to the grid.
THE NEW ROW HAS CLASS 'sg-grid-filter-row' THAT I USE FOR TESTING
*/
buildGridFilterRow(elm, scope, attrs);
});
}
Вы пробовали что-то вроде '$ timeout.flush (100)' для имитации прошедшего времени? – idursun
Куда бы я это сказал? После или до блокировки таймаута? – Tone
в любом месте после '' компиляции'', а также вам не нужен блок '$ timeout', вы должны иметь возможность утверждать напрямую. – idursun