UI Router Extras 破坏了我的单元测试并出现意外结果错误?

2023-11-27

问题:

- 安装 ui-router-extras (不是普通的 ui-router)时,为什么我的测试失败?

- 我该如何使用ui-router-extras我的测试仍然通过吗?


如果你想快速安装,请使用 yeoman + angular-fullstack-generator + Bower install ui-router-extras

我发现普通的 ui-router 也有类似的问题.

  • 幸运的是, ui-router 正常情况在我的测试中工作得很好。
  • 安装后ui-路由器附加功能我得到一个ERROR

Error log

If I uninstall ui-router.extras it this test passes just fine: enter image description here

更新了 $urlRouterProvider TEST 的 beforeEach 模块这是我的测试:

'use strict';

describe('Controller: MainCtrl', function () {

  // load the controller's module
  beforeEach(module('morningharwoodApp'));
  beforeEach(module('socketMock'));

  var MainCtrl,
      scope,
      $httpBackend;

  // Initialize the controller and a mock scope


  beforeEach(
    inject( function (_$httpBackend_, $controller, $rootScope) {
      $httpBackend = _$httpBackend_;
      $httpBackend.expectGET('/api/things')
        .respond(['HTML5 Boilerplate', 'AngularJS', 'Karma', 'Express']);

      scope = $rootScope.$new();
      MainCtrl = $controller('MainCtrl', {
        $scope: scope
      });
    }),
    module(function ($urlRouterProvider) {
      $urlRouterProvider.otherwise( function(){ return false; });
    })
  );



  it('should attach a list of things to the scope', function () {
    $httpBackend.flush();
    expect(scope.awesomeThings.length).toBe(4);
  });
});

这是我的karma.conf

module.exports = function(config) {
  config.set({
    // base path, that will be used to resolve files and exclude
    basePath: '',

    // testing framework to use (jasmine/mocha/qunit/...)
    frameworks: ['jasmine'],

    // list of files / patterns to load in the browser
    files: [
      'client/bower_components/jquery/dist/jquery.js',
      'client/bower_components/angular/angular.js',
      'client/bower_components/angular-mocks/angular-mocks.js',
      'client/bower_components/angular-resource/angular-resource.js',
      'client/bower_components/angular-cookies/angular-cookies.js',
      'client/bower_components/angular-sanitize/angular-sanitize.js',
      'client/bower_components/lodash/dist/lodash.compat.js',
      'client/bower_components/angular-socket-io/socket.js',
      'client/bower_components/angular-ui-router/release/angular-ui-router.js',
      'client/bower_components/famous-polyfills/classList.js',
      'client/bower_components/famous-polyfills/functionPrototypeBind.js',
      'client/bower_components/famous-polyfills/requestAnimationFrame.js',
      'client/bower_components/famous/dist/famous-global.js',
      'client/bower_components/famous-angular/dist/famous-angular.js',
      'client/app/app.js',
      'client/app/app.coffee',
      'client/app/**/*.js',
      'client/app/**/*.coffee',
      'client/components/**/*.js',
      'client/components/**/*.coffee',
      'client/app/**/*.jade',
      'client/components/**/*.jade',
      'client/app/**/*.html',
      'client/components/**/*.html'
    ],

    preprocessors: {
      '**/*.jade': 'ng-jade2js',
      '**/*.html': 'html2js',
      '**/*.coffee': 'coffee',
    },

    ngHtml2JsPreprocessor: {
      stripPrefix: 'client/'
    },

    ngJade2JsPreprocessor: {
      stripPrefix: 'client/'
    },

    // list of files / patterns to exclude
    exclude: [],

    // web server port
    port: 8080,

    // level of logging
    // possible values: LOG_DISABLE || LOG_ERROR || LOG_WARN || LOG_INFO || LOG_DEBUG
    logLevel: config.LOG_INFO,


    // enable / disable watching file and executing tests whenever any file changes
    autoWatch: false,


    // Start these browsers, currently available:
    // - Chrome
    // - ChromeCanary
    // - Firefox
    // - Opera
    // - Safari (only Mac)
    // - PhantomJS
    // - IE (only Windows)
    browsers: ['PhantomJS'],


    // Continuous Integration mode
    // if true, it capture browsers, run tests and exit
    singleRun: false
  });
};

这可能是由于某些组件依赖于$state在这种情况下$state将被实例化并执行默认路由。这就是为什么你的控制器之一的模板main.html正在获取。

要绕过此问题,请替换go() and transitionTo() of $state使用假人的方法:

beforeEach( inject( function ( _$state_ ) {
        state = _$state_;
        spyOn( state, 'go' );
        spyOn( state, 'transitionTo' );
    } ) );
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

UI Router Extras 破坏了我的单元测试并出现意外结果错误? 的相关文章

随机推荐