$timeout中的以下代码从未被调用。我可以在其中输入任何我喜欢的错误测试,并且测试总是通过(有一个类似的问题(Karma e2e testing: how to know when the DOM is ready?),但它没有提供解决方案):
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):871 (跳过383)中的1成功(0秒/ 0 )
这就是浏览器中的样子:

kendo网格是使用标准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);
});
}发布于 2014-01-24 10:08:07
你能显示你的测试代码吗?因为您必须等待以执行超时或只使用$timeout.flush();下面是一个示例:Unit testing an asynchronous service in angularjs
发布于 2014-02-06 10:23:44
终于(两周后!)把这个弄好了。@idursun和igorzg建议使用$timeout.flush(100)是正确的,但正如@idursun在他的评论中进一步提到的那样,它也需要删除$timeout块。
当我最初尝试这个时,它没有起作用,但现在起作用了。别问我为什么,我只关心它是否起作用!以下是我的完整测试用例供参考:
it('should display a filter row when attribute sg-live-filtering is present', function () {
angular.mock.inject(function($compile, $rootScope, $timeout) {
var scope = $rootScope.$new();
scope.accountColumnsForAdvSearch = [
{field: 'accountId', title: 'AccountId', dataType: 'string'},
{field: 'name', title: 'Account Name', dataType: 'string'},
{field: 'shortName', title: 'Short Name', dataType: 'string'},
{field: 'status', title: 'Status', dataType: 'string'}
];
$httpBackend.when('GET', 'api/grid/accounts').respond(accountData);
var elem = $compile('<div sg-grid sg-data="api/grid/accounts" sg-columns="accountColumnsForAdvSearch" sg-live-filtering="true"></div>')(scope);
$rootScope.$apply();
$httpBackend.flush();
$timeout.flush(100); // wait for DOM to load
var table = elem.find('table[role="grid"]'); // the kendo grid
expect(table.length).toBe(1);
var filterRow = table.find('.sg-grid-filter-row'); // the filter row
expect(filterRow.length).toBe(1);
});https://stackoverflow.com/questions/21329194
复制相似问题