如果以某种方式在TypeScript中编写角度控制器,以便控制器接受$scope
作为输入参数:
class TestCtrl {
constructor($scope:ng.IScopeService) {
$scope.myData = "Information";
}
}
您可以通过以下方式轻松验证范围内的属性:
beforeEach(function() {
$controller("TestCtrl", { $scope: $scope });
});
it("Should have some data set upon creation", function() {
expect($scope.myData).not.toBeUndefined(); // this is perfectly fine, and we can assert the data.
});
你也可以用这种方式创建控制器, not 在构造函数中提供$ scope:
interface ITestScope {
myData: string;
}
class TestCtrl implements ITestScope {
myData: string; // this will be available on the scope as well.
constructor() {
this.myData = "Information";
}
}
现在从我的测试中,当我无法再访问$scope
时,如何访问此范围并验证数据?
答案 0 :(得分:1)
您没有在控制器中注入作用域(constructor($scope:ng.IScopeService) {
),因此您无需在$controller
的本地区域中传递它。看起来你正在使用controller as
语法(因为你在控制器实例上分配属性)所以只需使用$controller
服务返回的控制器实例。
即
var ctrl;
beforeEach(function() {
ctrl = $controller("MyCtrl", {}); //No locals and assign result to a variable
});
it("Should have some data set upon creation", function() {
expect(ctrl.myData).not.toBeUndefined(); // Set your expectation on controller instance
//expect(ctrl.myData).toBeDefined() //This is just as same...
});