2016-05-12 90 views
0

I所定義的具有樣品的角應用如下面

(function() { 

    angular.module("WatchApp", []) 
     .controller("WatchController", function ($scope) { 
      $scope.message = "hello"; 
     }); 
}()); 

我有一個測試的情況下像下面

describe("WatchController", function() { 

    var $scope; 

    beforeEach(module("WatchApp")); 
    beforeEach(
     inject(function (_$controller_) { 
      $scope: {}; 
      controller: _$controller_("WatchController", { 
       $scope: $scope 
      }); 

     })); 

    describe("Initialization", function() { 
     it("Should be truthy", function() { 
      expect(true).toBeTruthy(); 
     }) 
    }) 

}); 

我知道測試用例是不對,只是寫了看看配置是否正確,但總是給出如下錯誤:

PhantomJS 2.1.1 (Windows 7 0.0.0) WatchController Initialization Should be truthy FAILED 
     TypeError: undefined is not an object (evaluating '$scope.message = "hello"') (line 5) 
     C:/robin/Studies/Angularjs/ut/app/controllers/watchController.js:5:19 
     [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:9: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:7:15 
     global [email protected]:/robin/Studies/Angularjs/ut/test/controllers/watchControllerSpec.js:1:9 

請讓我知道了什麼是問題

回答

0

試試這個

inject(function (_$controller_, _$scope_) { 
     $scope: _$scope_; 
     controller: _$controller_("WatchController", { 
      '$scope': $scope 
     }); 

    })); 
+0

它拋出以下異常。 錯誤:[$ injector:unpr]未知的提供者:$ scopeProvider < - $ scope – robin

+0

Woops,改變了上面的一些東西,給它一個去嗎? – mwild

+0

同樣問題仍然 – robin

0

只要確保注入並在測試使用$rootScope正常。

describe("WatchController", function() { 

    var $scope, 
     $controller; 

    beforeEach(module("WatchApp")); 
    beforeEach(
    inject(function (_$controller_, _$rootScope_) { 
     $scope = _$rootScope_.$new(); 
     $controller = _$controller_("WatchController", { 
     $scope: $scope 
     }); 
    })); 

    describe("Initialization", function() { 
    it("Should be truthy", function() { 
     expect(true).toBeTruthy(); 
    }); 

    it("should set a message", function() { 
     expect($scope.message).toBe("hello"); 
    }); 
    }); 
}); 
+0

不需要root作用域它是因爲我使用:而不是= to – robin

0

發行固定通過改變

inject(function (_$controller_) { 
      $scope: {}; 
      controller: _$controller_("WatchController", { 
       $scope: $scope 
      }); 

inject(function (_$controller_) { 
      $scope= {}; 
      controller: _$controller_("WatchController", { 
       $scope: $scope 
      });