【问题标题】:angularJS $stateProvider : How to unit test different views in UI-Router?angularJS $stateProvider:如何在 UI-Router 中对不同的视图进行单元测试?
【发布时间】:2016-03-06 07:57:01
【问题描述】:

如何对以下场景的不同视图进行单元测试

.state('app.sr.product.upload', {
            name: 'upload',
            url: '/upload',
            data: {
                tags: [],
                userCommunities: []
            },
            views: {
                "productView@app.sr.product": {
                    templateUrl: 'views/upload/upload.html',
                    controller: 'UploadCtrl',
                    controllerAs: 'ul'
                },
                "tags@app.sr.product.upload": {
                    templateUrl: 'views/tags/tags.html',
                    controller: 'TagsCtrl',
                    controllerAs: 'vm'
                },
                "UserCommunityPanel@app.sr.product.upload": {
                    templateUrl: 'views/user-community/user-community.html',
                    controller: 'UserCommunityCtrl',
                    controllerAs: 'ul'
                },
            }
        })
  • 如果我的观点是 tags@app.sr.product.upload 那么我该如何测试呢 我的控制器是 TagsCtrl,我的 controllerAs 值是 vm 等等??

  • 如果我的状态是 app.sr.product.upload 那么如何进行单元测试 data.tags=[], data.userCommunities=[]等

我搜索了很多文档和教程,但没有找到。

任何帮助都是可观的。 谢谢

【问题讨论】:

    标签: angularjs unit-testing angular-ui-router angular-templatecache


    【解决方案1】:

    试穿它的大小。我假设您将使用 jasmine 进行测试,但任何测试框架的概念都是相同的。

    运行测试时,首先订阅'$stateChangeSuccess' 事件,然后导航到该状态。事件触发后,检查 toState 值以查看它们是否符合您的预期。

    您可以运行 sn-p 来查看正在运行的测试。

    //write a unit test
    describe('state changes', function() {
      beforeEach(module('app'));
      var $rootScope, $state;
      beforeEach(inject(function(_$rootScope_, _$state_) {
        // The injector unwraps the underscores (_) from around the parameter names when matching
        $rootScope = _$rootScope_;
        $state = _$state_;
      }));
    
    
      it('loads page 1', function(done) {
        //wait for the state to change, then make sure we changed to the correct state
        $rootScope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams) {
          expect(toState.controller).toEqual('Controller1');
          done();
        });
        //navigate to the state
        $state.go('state1');
        //start a digest cycle so ui-router will navigate
        $rootScope.$apply();
      });
    
      it('loads page 2', function(done) {
        //wait for the state to change, then make sure we changed to the correct state
        $rootScope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams) {
          expect(toState.controller).toEqual('Controller2');
          done();
        });
        //navigate to the state
        $state.go('state2');
        //start a digest cycle so ui-router will navigate
        $rootScope.$apply();
      });
    
      it('loads page 3', function(done) {
        //wait for the state to change, then make sure we changed to the correct state
        $rootScope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams) {
          expect(toState.controller).toEqual('Controller3');
          done();
        });
        //navigate to the state
        $state.go('state3');
        //start a digest cycle so ui-router will navigate
        $rootScope.$apply();
      });
    });
    
    //set up some dummy controllers and some dummy states
    angular.module('app', ['ui.router']).controller('Controller1', function() {
      this.message = 'Page 1';
    }).controller('Controller2', function() {
      this.message = 'Page 2';
    }).controller('Controller3', function() {
      this.message = 'Page 3';
    }).config(function($stateProvider, $urlRouterProvider) {
      $urlRouterProvider.otherwise("/state1");
    
      $stateProvider.state('state1', {
        url: "/state1",
        controller: 'Controller1',
        controllerAs: 'vm',
        template: '<h1>{{vm.message}}</h1>'
      }).state('state2', {
        url: "/state2",
        controller: 'Controller2',
        controllerAs: 'vm',
        template: '<h2>{{vm.message}}</h2>'
      }).state('state3', {
        url: "/state3",
        controller: 'Controller3',
        controllerAs: 'vm',
        template: '<h3>{{vm.message}}</h3>'
      });
    });
    h1 {
      color: red;
    }
    h2 {
      color: blue;
    }
    h3 {
      color: green;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.min.js"></script>
    
    <script src="
    https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.18/angular-ui-router.js"></script>
    <link rel="stylesheet" type="text/css" href="http://jasmine.github.io/2.0/lib/jasmine.css">
    <script src="http://jasmine.github.io/2.0/lib/jasmine.js"></script>
    <script src="http://jasmine.github.io/2.0/lib/jasmine-html.js"></script>
    <script src="http://jasmine.github.io/2.0/lib/boot.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular-mocks.js"></script>
    <div ng-app="app">
      <a ui-sref="state1">State 1</a>
      <a ui-sref="state2">State 2</a>
      <a ui-sref="state3">State 3</a>
      <div ui-view></div>
    </div>

    【讨论】:

    • 这些测试的问题在于,如果从未调用过 $stateChangeSuccess 事件处理函数,它们可能会产生误报。
    • @ShaunScovil,我认为他们不会产生误报。因为他们正在使用 done 回调,如果没有调用 done 函数,jasmine 将在超时后抛出错误。所以是的,测试可能需要很长时间才能运行,但 jasmine 会让你知道你的测试从未说过它已经完成。
    • 如果由于某种原因未调用事件处理函数,则回调之外没有会导致测试失败的 expect()。
    • 为什么我收到错误:意外请求:GET views/index/index.html 没有更多的请求预期.. 错误
    • 您的路由器可能正在尝试加载索引页面,这很可能是您的默认页面。您可以为该文件添加一个拦截器并忽略它。 $httpBackend.when('GET', 'views/index/index.html') .respond("&lt;h1&gt;I am the index&lt;/h1&gt;");
    【解决方案2】:

    如果我没记错的话,我认为我们错过了最初问题的重点,即

    如果我的观点是 tags@app.sr.product.upload 那么我该如何测试我的 控制器是TagsCtrl,我的controllerAs值是vm等??

    和

    如果我的状态是 app.sr.product.upload 那么我如何进行单元测试 data.tags=[]、data.userCommunities=[] 等

    以下是测试这些的方法:

    var $rootScope, $state, $injector, state;
    
    beforeEach(inject(function(_$rootScope_, _$state_){
        $rootScope = _$rootScope_;
        $state = _$state_;
        state = $state.get('app.sr.product.upload');
    }));
    
    it('should have the correct data parameters', function () {
    
        expect(state.data.tags).toEqual('');
        expect(state.data.userCommunities).toEqual('');
    
    });
    
    it('should render the dashboard views with the right Controllers', function () {
    
        var product = state.views['productView@app.sr.product'];
        var tags= state.views['tags@app.sr.product.upload'];
        var userCommunity = state.views['UserCommunityPanel@app.sr.product.upload'];
    
        expect(product.templateUrl).toEqual('views/upload/upload.html');
        expect(product.controller).toEqual('UploadCtrl');
        expect(product.controllerAs).toEqual('ul');
    
        // etc...
    
    });
    

    此外,在较新的 Angular 版本中,您可以像这样声明您的控制器:

    controller: 'UploadCtrl as vm'
    

    【讨论】:

      【解决方案3】:

      这不是我通常会进行单元测试的东西。 UI-Router 本身经测试为well covered。

      使用Protractor 进行 e2e(端到端)测试会更好。您模拟点击链接,您希望 url 是这样,使用预期列表中的元素数量是这样的等等。

      但如果你真的需要它:

      • 定位每个视图的根元素(例如,通过添加特定类和使用选择器)
      • 您应该能够访问范围 和控制器通过 angular.element 包装方法

      【讨论】:

      • 能否说的更清楚一些,如果能提供一些demo代码或者任何例子,那将是一个很大的帮助
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-24
      • 1970-01-01
      • 2014-09-21
      • 2014-04-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多