首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UI路由器Extras以意外的结果错误破坏我的单元测试?

UI路由器Extras以意外的结果错误破坏我的单元测试?
EN

Stack Overflow用户
提问于 2014-12-31 18:31:15
回答 4查看 2.1K关注 0票数 6

问题:

-为什么我的测试在用户界面路由器附加(非普通用户界面路由器)安装时失败?

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

如果您想要快速安装此设备,请使用yeoman +角全堆栈生成器+ bower安装ui-路由器附加程序。

我在普通用户界面路由器上发现了类似的问题。

  • 幸运的是,ui-路由器正常工作在我的测试中。
  • 安装ui-路由器-附加程序后,我得到一个错误

如果我卸载ui-router.interas,这个测试通过得很好:

为$urlRouterProvider测试的beforeEach模块更新了我的测试:

代码语言:javascript
复制
'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

代码语言:javascript
复制
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
  });
};
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2015-01-09 07:56:08

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

要绕过这一点,请将go()transitionTo()$state方法替换为假人:

代码语言:javascript
复制
beforeEach( inject( function ( _$state_ ) {
        state = _$state_;
        spyOn( state, 'go' );
        spyOn( state, 'transitionTo' );
    } ) );
票数 10
EN

Stack Overflow用户

发布于 2015-01-13 18:05:33

这里有一个不使用ui-路由器的transitionTo功能的替代解决方案。

首先,可以通过以下步骤再现失败的场景:

代码语言:javascript
复制
npm install yo generator-angular-fullstack;
yo angular-fullstack

通过将这一行添加到client/app/app.js中,注入$state服务:

代码语言:javascript
复制
angular.module("yoAppName").run(function($state) {});

此时,调用grunt karma会报告Unexpected request: GET app/main/main.html,因为UI-路由器被引导并尝试加载默认路由,该路由请求路由的模板。

要解决这个问题,请告诉UI-路由器延迟同步到状态的URL,这样我们就不会加载默认路由。在控制器规范中,将$urlRouterProvider.deferIntercept();添加到测试init代码中:

代码语言:javascript
复制
beforeEach(module('uiRouterExtrasKarmaBugApp'));
// Add the following line
beforeEach(module(function($urlRouterProvider) { $urlRouterProvider.deferIntercept(); }));
票数 4
EN

Stack Overflow用户

发布于 2014-12-31 18:45:02

1)您的测试失败了,因为ui-router-extrasapp/main/main.html发出了意外的http请求,因此测试失败。

2)事实上,在你所链接的问题上,有很多建议。我假设有额外的调用来加载默认路由的模板,即。otherwise。因此,覆盖它可能会解决问题:

代码语言:javascript
复制
beforeEach(module(function ($urlRouterProvider) {
    $urlRouterProvider.otherwise(function(){return false;});
}));
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27724956

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档