2016-05-13 18 views
1

我有類似下面問題與角UT在

(function() { 
    angular.module("WatchApp", []) 
     .controller("WatchController", function ($scope) { 
      $scope.options = { 
       rowSelection: true 
       , multiSelect: false 
       , autoSelect: false 
       , decapitate: false 
       , largeEditDialog: false 
       , boundaryLinks: false 
       , limitSelect: true 
       , pageSelect: true 
      }; 

      $scope.$watch($scope.options.rowSelection, function (newValue, oldValue) { 
       if (!oldValue) { 
        console.log("! old Value "); 
       } 

       if (newValue !== oldValue) { 
        console.log("newValue != old Value "); 
       } 

       if (!newValue) { 
        console.log("! newValue"); 
       } 

      }); 
     }); 
}()); 

也對我有UT此控制器就像一個控制器,該控制器具有一個鐘錶時,下面

describe("WatchController", function() { 
    var $scope; 
    beforeEach(function() { 
     module("WatchApp"); 
     return null; 
    }); 
    beforeEach(
     inject(function (_$controller_) { 
      $scope = {}; 
      controller: _$controller_("WatchController", { 
       $scope: $scope 
      }); 

     })); 
    describe("Initialization", function() { 
     it("newPlace.city and country should be empty", function() { 
      expect($scope.options.rowSelection).toEqual(true); 
     }) 
    }); 

}); 

這UT將正常工作如果我刪除$ scope。$ watch塊,否則我會得到下面的異常。

PhantomJS 2.1.1 (Windows 7 0.0.0) WatchController Initialization newPlace.city and country should be empty F 
     TypeError: undefined is not a constructor (evaluating '$scope.$watch') (line 15) 
     C:/Robin/Studies/Angularjs/ut/app/controllers/watchController.js:15:26 
     [native code] 
     [email protected]:/Robin/Studies/Angularjs/ut/bower_components/angular/angular.js:4680:61 
     [email protected]:/Robin/Studies/Angularjs/ut/bower_components/angular/angular.js:10130:39 
     C:/Robin/Studies/Angularjs/ut/bower_components/angular-mocks/angular-mocks.js:2194:21 
     C:/Robin/Studies/Angularjs/ut/test/controllers/watchControllerSpec.js:10:38 
     [email protected]:/Robin/Studies/Angularjs/ut/bower_components/angular/angular.js:4665:24 
     [email protected]:/Robin/Studies/Angularjs/ut/bower_components/angular-mocks/angular-mocks.js:2965:26 
     [email protected]:/Robin/Studies/Angularjs/ut/bower_components/angular-mocks/angular-mocks.js:2931:28 
     C:/Robin/Studies/Angularjs/ut/test/controllers/watchControllerSpec.js:8:15 
     global [email protected]:/Robin/Studies/Angularjs/ut/test/controllers/watchControllerSpec.js:1:9 
PhantomJS 2.1.1 (Windows 7 0.0.0): Executed 14 of 14 (1 FAILED) (0.016 secs/0.39 secs) 

回答

0

更新手錶線路碼爲:

$scope.$watch('options.rowSelection', function (newValue, oldValue) { 

更詳細地呈現本link

+0

我明白你的意思。但那也是拋出同樣的例外。 – robin

0

問題上手錶例子的外觀是通過修改等

inject(function (_$controller_, $rootScope) { 
      $scope = $rootScope.$new(); 
      controller: _$controller_("WatchController", { 
       $scope: $scope 
      }); 
噴出方法固定