2014-03-31 55 views
0

我是新來的角,我試圖得到一個測試的處理,但已經在一個看似愚蠢的問題難住了一段時間,也許這是一個缺乏理解$編譯。我已經煮了我的單元測試代碼到以下幾點:

it('should create a table', inject(['$compile', '$rootScope', function ($compile, $rootScope) { 
    var element; 
    var scope = $rootScope.$new(); 
    scope.list = [{name: 'tst'},{name:'sae'},{name:'dkos'}] 
    element = angular.element('<ul><li ng-repeat="p in list">{{p.name}}</li></ul>'); 
    element = $compile(element)(scope); 
    expect(element.find('li').length).to.equal(3); 
}])); 

當我運行因果報應我收到以下故障:

$ grunt karma:unit 
Running "karma:unit" (karma) task 
INFO [karma]: Karma v0.10.10 server started at http://localhost:9876/ 
INFO [launcher]: Starting browser Chrome 
INFO [Chrome 33.0.1750 (Mac OS X 10.9.2)]: Connected on socket 6K3QkOOLMEYRkZco3rY3 
Chrome 33.0.1750 (Mac OS X 10.9.2) Directive: ngTabled should create a table FAILED 
    AssertionError: expected 0 to equal 3 
     at Context.<anonymous> (/Users/andyperlitch/js/ng-tabled/test/spec/directives/ng-tabled.js:22:44) 
     at Object.invoke (/Users/andyperlitch/js/ng-tabled/app/bower_components/angular/angular.js:3697:17) 
     at Context.workFn (/Users/andyperlitch/js/ng-tabled/app/bower_components/angular-mocks/angular-mocks.js:2102:20) 
     at callFn (/Users/andyperlitch/js/ng-tabled/node_modules/mocha/mocha.js:4338:21) 
     at Test.Runnable.run (/Users/andyperlitch/js/ng-tabled/node_modules/mocha/mocha.js:4331:7) 
     at Runner.runTest (/Users/andyperlitch/js/ng-tabled/node_modules/mocha/mocha.js:4728:10) 
     at /Users/andyperlitch/js/ng-tabled/node_modules/mocha/mocha.js:4806:12 
     at next (/Users/andyperlitch/js/ng-tabled/node_modules/mocha/mocha.js:4653:14) 
     at /Users/andyperlitch/js/ng-tabled/node_modules/mocha/mocha.js:4663:7 
     at next (/Users/andyperlitch/js/ng-tabled/node_modules/mocha/mocha.js:4601:23) 
    Error: Declaration Location 
     at window.inject.angular.mock.inject (/Users/andyperlitch/js/ng-tabled/app/bower_components/angular-mocks/angular-mocks.js:2087:25) 
     at Suite.<anonymous> (/Users/andyperlitch/js/ng-tabled/test/spec/directives/ng-tabled.js:16:31) 
     at context.describe.context.context (/Users/andyperlitch/js/ng-tabled/node_modules/mocha/mocha.js:955:10) 
     at /Users/andyperlitch/js/ng-tabled/test/spec/directives/ng-tabled.js:3:1 
Chrome 33.0.1750 (Mac OS X 10.9.2): Executed 1 of 1 (1 FAILED) ERROR (0.21 secs/0.029 secs) 
Warning: Task "karma:unit" failed. Use --force to continue. 

Aborted due to warnings. 


Execution Time (2014-03-31 06:41:05 UTC) 
karma:unit 2.5s ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 100% 
Total 2.5s 

我撕裂我的頭髮了這一點,所以任何幫助會太好了!如果有必要,我很樂意提供更多細節。

安迪

回答

4

它看起來像它需要scope.$digest()來電順序調用$compile()爲的ng-repeat後,在列表上運行。所以,你的測試可以像:

it('should create a table', inject(['$compile', '$rootScope', function ($compile, $rootScope) { 
    var element; 
    var scope = $rootScope.$new(); 
    scope.list = [{name: 'tst'},{name:'sae'},{name:'dkos'}] 
    element = angular.element('<ul><li ng-repeat="p in list">{{p.name}}</li></ul>'); 
    element = $compile(element)(scope); 
    scope.$digest(); 
    expect(element.find('li').length).toEqual(3); 
}])); 

這可以在行動在http://plnkr.co/edit/ypDZeSPMggknqsuiETh8?p=preview

+0

可以看出謝謝了! – AndyPerlitch