Angular UI-Router 测试范围继承 [英] Angular UI-Router testing scope inheritance

查看:21
本文介绍了Angular UI-Router 测试范围继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为嵌套状态编写单元测试.状态从父状态继承一些范围元素.在我的测试中,我正在创建控制器,它显然没有父状态的可见性,因此也没有继承的范围项的可见性.

I am trying to write units tests for a nested state. The state inherits some scope elements from the parent state. In my tests I am creating the controller, which obviously has no visibility of the parent state, and therefore no visibility of the inherited scope items.

it('should show the account page', inject(['$controller', 'security', function ($controller, security){
  // set up controller with a scope
  var $scope = $rootScope.$new();

  // the parent controller calls this
  var user = { first: 'First', last: 'Last', email: 'testuser@test.com', gender:'male', hasPassword:true};
  $httpBackend.when('GET', '/api/user/current-user').respond(200, user);
  $controller('AccountViewCtrl', { $scope: $scope});

  $rootScope.$digest();

  // verify the initial setup
  //scope attributes defined on the parent controller
  expect($scope.user).toEqual(user);
  expect($scope.account).toEqual(user);

  // scope attribute defined on the controller
  expect($scope.genders.length).toEqual(2);
  expect($scope.editable).toBe(false);
  expect($scope.editCheck()).toBe(false);
}]));

AccountCtrl 定义了所有子状态所需的许多元素.

The AccountCtrl defines a number of elements needed by all child states.

.controller('AccountCtrl', ['$scope', 'security', function ($scope, security){
  $scope.user = security.requestCurrentUser();
  $scope.account = angular.copy($scope.user);
}])

AccountViewCtrl 定义了其他元素,但继承了父 AccountCtrl 的元素

The AccountViewCtrl defines the other elements, but inherits the elements from the parent AccountCtrl

.controller('AccountViewCtrl', ['$scope', function ($scope) {
  $scope.editable = false;
  $scope.genders = [
    { name: 'Male', value: 'male' },
    { name: 'Female', value: 'female' }
  ];

  $scope.editCheck = function(){
    return $scope.editable;
  };

  ....
}])

测试失败,因为我只是在实例化控制器,并且没有父状态和关联元素的可见性.是否有测试 ui-router 状态的最佳实践方法?

The tests are failing as I am only instantiating the controller and there is no visibility of the parent state and associated elements. Is there a best practice approach to testing ui-router states?

推荐答案

作用域继承是 Angular 的一个特性.因此,我认为没有必要在我的测试中进行测试.

Scope inheritance is a feature of Angular. I therefore think that it is not necessary to test in my tests.

我的解决方法是测试 AccountCtrl 是否有效,然后将值放入其他测试中:

My workaround is to test that the AccountCtrl works and then just put the values in the other tests:

it("should set scope when account Ctrl loaded", inject(['$controller', 'security', function($controller, security){
  // Add your behavior testing code here
  var $scope = $rootScope.$new();
  var user = { first: 'First', last: 'Last', email: 'testuser@test.com', gender:'male', birthday:'password'};
  security.currentUser = user;
  $controller('AccountCtrl', { $scope: $scope});
  $rootScope.$digest();

  // verify the initial setup
  expect($scope.user).toEqual(user);
  expect($scope.account).toEqual(user);
}]));

然后在其他测试中我手动填充值:

Then in the other tests I manually populate the values:

it('should set scope on account page', inject(['$controller', 'I18N.MESSAGES', function ($controller, i18nMessages){
  // set up controller with a scope
  var $scope = $rootScope.$new();
  var user = { first: 'First', last: 'Last', email: 'testuser@test.com', gender:'male', birthday:'password'};
  $scope.account = $scope.user = user;
  ...
}]));

这篇关于Angular UI-Router 测试范围继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆