Jasmine Angular 单元测试:带有外部变量的控制器
Jasmine Angular unit testing: controller with an external variable
我正在尝试为使用在控制器外部定义的 $scope.action 变量的控制器编写单元测试。
controller( "MyController", [
"$scope", "$window", "httpInterceptor",
function($scope, Service1, Service2, $window, httpInterceptor) {
$scope.init = function() { ... });
$scope.action.cb.write = function() { ... };
$scope.action.cb.read = function() { ... };
}]
);
我的单元测试如下
describe("The controller", function () {
var $controller, $scope, httpInterceptor, window, httpBackend;
beforeEach(function() {
inject(function(_httpInterceptor_, _$controller_, _$rootScope_, $httpBackend, $http) {
$scope = _$rootScope_.$new();
httpInterceptor = _httpInterceptor_;
httpBackend = $httpBackend;
window = {setTimeout: function() {}};
$controller = _$controller_("MyController", {
httpInterceptor: _httpInterceptor_, $scope: $scope, $window: window, $http: $http
});})});
afterEach(function() {
httpBackend.verifyNoOutstandingExpectation();
httpBackend.verifyNoOutstandingRequest();
});
describe("The init() method", function () {
it("Init method", function () {
$scope.$parent.action = {}
$scope.init();
});});});
我收到一个错误 TypeError: Cannot read property "cb" from undefined
。我很困惑为什么我会收到此错误,因为我试图仅测试 init 函数。我最初尝试将 $scope.$parent.action = {}
模拟为 $scope.action = {}
但无济于事。
谢谢。
因为在控制器获取 "instantiated" 时引用了您的 $scope.action
,所以在您的测试中分配它实际上为时已晚,因为 describe/it 块在 [=16= 之后运行] 块完成。你应该在你的注入中初始化它。
inject(function(_httpInterceptor_, _$controller_, _$rootScope_, $httpBackend, $http) {
$scope = _$rootScope_.$new();
$scope.action = {};
定义 $scope
属性,然后将其注入到测试中的控制器中:
$scope = _$rootScope_.$new();
$scope.action = {'cb': {}};
我正在尝试为使用在控制器外部定义的 $scope.action 变量的控制器编写单元测试。
controller( "MyController", [
"$scope", "$window", "httpInterceptor",
function($scope, Service1, Service2, $window, httpInterceptor) {
$scope.init = function() { ... });
$scope.action.cb.write = function() { ... };
$scope.action.cb.read = function() { ... };
}]
);
我的单元测试如下
describe("The controller", function () {
var $controller, $scope, httpInterceptor, window, httpBackend;
beforeEach(function() {
inject(function(_httpInterceptor_, _$controller_, _$rootScope_, $httpBackend, $http) {
$scope = _$rootScope_.$new();
httpInterceptor = _httpInterceptor_;
httpBackend = $httpBackend;
window = {setTimeout: function() {}};
$controller = _$controller_("MyController", {
httpInterceptor: _httpInterceptor_, $scope: $scope, $window: window, $http: $http
});})});
afterEach(function() {
httpBackend.verifyNoOutstandingExpectation();
httpBackend.verifyNoOutstandingRequest();
});
describe("The init() method", function () {
it("Init method", function () {
$scope.$parent.action = {}
$scope.init();
});});});
我收到一个错误 TypeError: Cannot read property "cb" from undefined
。我很困惑为什么我会收到此错误,因为我试图仅测试 init 函数。我最初尝试将 $scope.$parent.action = {}
模拟为 $scope.action = {}
但无济于事。
谢谢。
因为在控制器获取 "instantiated" 时引用了您的 $scope.action
,所以在您的测试中分配它实际上为时已晚,因为 describe/it 块在 [=16= 之后运行] 块完成。你应该在你的注入中初始化它。
inject(function(_httpInterceptor_, _$controller_, _$rootScope_, $httpBackend, $http) {
$scope = _$rootScope_.$new();
$scope.action = {};
定义 $scope
属性,然后将其注入到测试中的控制器中:
$scope = _$rootScope_.$new();
$scope.action = {'cb': {}};