2014-01-24 114 views
2

$超时中的以下代码永远不会被调用。我可以把任何错误的测试,我喜欢在那里和测试总是通过(有一个类似的问题(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中):执行的8711(跳过383)SUCCESS(0秒/ 0

这就是它看起来像在浏览器中:

enter image description here

剑道网格创建使用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-实时过滤attr为存在。如果是,效用函数被调用,以追加看到高亮显示的图像,以网格的表头行:

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); 
    }); 
} 
+2

您是否试过类似'$ timeout.flush(100)'来模拟传递时间? – idursun

+0

我会在那里放?在超时块之后或之前? – Tone

+1

'$ compile'应该做的任何地方,并且你也不需要'$ timeout'块,你应该可以直接声明。 – idursun

回答

2

可以显示你的测试代码??? 因为您必须等待才能执行超时或仅使用$ timeout.flush(); 这里是一个例子: Unit testing an asynchronous service in angularjs

+0

你可以在这个问题的第一个灰色块中看到我的测试代码(它('should ...)。$ timeout.flush()被idursun推荐,但是我不能 – Tone

+0

使用茉莉花的runs和waitsFor方法来等待代码执行 – igorzg

+0

好的,谢谢我会在$ timeout中尝试那些 – Tone

2

最后(2周后!)得到了这个工作。 @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); 
}); 
+0

sg-grid有什么用途吗?http://kendo-labs.github.io/angular-kendo以外的东西是什么? –

+0

@WayneBrantley - 刚刚看到您的评论sg-grid是kendo网格,这只是我们有我们自己的指令,称为sgGrid,然后负责创建剑道网格。 – Tone