2014-12-07 103 views
4

有一個視圖,該按鈕的角度指令ng禁用,綁定到某些模型屬性。複雜的是,該按鈕位於<li>標籤內。單元測試角度視圖

我想要做的是設置模型屬性,編譯視圖,然後根據模型屬性的值檢查按鈕是否被正確啓用或禁用;但是,當我嘗試編譯時,列表爲空:僅有< ul標籤。

有沒有辦法完成我想要做的事情?

這裏是我的代碼:

-----------------觀點-------------------

<div ng-show="group.AreItemsExpanded"> 
    <div> 
     <ul> 
      <li ng-repeat="item in group.Items"> 
       <div> 
        {{item.Name}} 
       </div> 
       <div> 
        <button id="btn" type="button" ng-click="clickItem(item)" 
          ng-disabled="!permissions.IsAllowed || !item.IsClickable"> 
         Click! 
        </button> 
       </div> 
      </li> 
     </ul> 
    </div> 
</div> 

-----------------測試------------------

describe('testing view', function(){ 
    beforeEach(module('groups.templates')); 

    var $scope; 
    var item; 
    var $templateCache; 
    var $compile; 

    beforeEach(inject(function($rootScope, _$templateCache_, _$compile_){ 
     $templateCache = _$templateCache_; 
     $compile = _$compile_; 
     item = { 
      Name: 'testitem0', 
      IsClickable: false 
     }; 
     $scope = $rootScope.$new(); 
     $scope.group = { 
      Items: [item], 
      AreItemsExpanded: true 
     }; 
     $scope.permissions = {IsAllowed: false}; 
    })); 

    describe('testing button', function(){ 
     it('should be enabled', function(){ 
      $scope.group.Items[0].IsClickable = true; 
      $scope.permissions.IsAllowed = true; 
      var html = $templateCache.get('view.html'); 
      var view = $compile(angular.element(html))($scope); 
      expect(view.find('button')[0].disabled).toEqual(false); 
     }); 
     it('should be disabled', function(){ 
      $scope.group.Items[0].IsClickable = false; 
      $scope.permissions.IsAllowed = true; 
      var html = $templateCache.get('view.html'); 
      var view = $compile(angular.element(html))($scope); 
      expect(view.find('button')[0].disabled).toEqual(true); 
     }); 
    }); 
}); 

- ------------ karma.conf.js ---------------------

// Karma configuration 
// Generated on Sat Dec 06 2014 13:06:16 GMT-0700 (Mountain Standard Time) 

module.exports = function(config) { 
    config.set({ 

    // base path that will be used to resolve all patterns (eg. files, exclude) 
    basePath: '', 


    // frameworks to use 
    // available frameworks: https://npmjs.org/browse/keyword/karma-adapter 
    frameworks: ['jasmine'], 


    // list of files/patterns to load in the browser 
    files: [ 
     'node_modules/angular/angular.js', 
     'node_modules/angular-mocks/angular-mocks.js', 
     'node_modules/angular-resource/angular-resource.js', 
     'node_modules/angular-route/angular-route.js', 
     'node_modules/angular-bootstrap/ui-bootstrap.js', 
     '*.js', 
     '*.html' 
    ], 


    // list of files to exclude 
    exclude: [ 
    ], 


    // preprocess matching files before serving them to the browser 
    // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor 
    preprocessors: { 
     "*.html": ["ng-html2js"] 
    }, 

    ngHtml2JsPreprocessor: { 
     moduleName: "groups.templates" 
    }, 


    // test results reporter to use 
    // possible values: 'dots', 'progress' 
    // available reporters: https://npmjs.org/browse/keyword/karma-reporter 
    reporters: ['progress'], 


    // web server port 
    port: 9876, 


    // enable/disable colors in the output (reporters and logs) 
    colors: true, 


    // level of logging 
    // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG 
    logLevel: config.LOG_INFO, 


    // enable/disable watching file and executing tests whenever any file changes 
    autoWatch: true, 


    // start these browsers 
    // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher 
    browsers: ['Chrome'], 


    // Continuous Integration mode 
    // if true, Karma captures browsers, runs the tests and exits 
    singleRun: false 
    }); 
}; 

回答

2

發現問題。所有這一切都在分配後失蹤了聲明

$scope.$digest(); 

var view = $compile(angular.element(html))($scope);