问题:
-为什么我的测试在用户界面路由器附加(非普通用户界面路由器)安装时失败?
ui-router-extras - -我如何使用而仍然通过测试?
如果您想要快速安装此设备,请使用yeoman +角全堆栈生成器+ bower安装ui-路由器附加程序。

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

为$urlRouterProvider测试的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
});
};发布于 2015-01-09 07:56:08
这可能是某个组件依赖于$state的结果,在这种情况下,将实例化$state并执行默认路由。这就是为什么要获取一个控制器main.html的模板。
要绕过这一点,请将go()和transitionTo()的$state方法替换为假人:
beforeEach( inject( function ( _$state_ ) {
state = _$state_;
spyOn( state, 'go' );
spyOn( state, 'transitionTo' );
} ) );发布于 2015-01-13 18:05:33
这里有一个不使用ui-路由器的transitionTo功能的替代解决方案。
首先,可以通过以下步骤再现失败的场景:
npm install yo generator-angular-fullstack;
yo angular-fullstack通过将这一行添加到client/app/app.js中,注入$state服务:
angular.module("yoAppName").run(function($state) {});此时,调用grunt karma会报告Unexpected request: GET app/main/main.html,因为UI-路由器被引导并尝试加载默认路由,该路由请求路由的模板。
要解决这个问题,请告诉UI-路由器延迟同步到状态的URL,这样我们就不会加载默认路由。在控制器规范中,将$urlRouterProvider.deferIntercept();添加到测试init代码中:
beforeEach(module('uiRouterExtrasKarmaBugApp'));
// Add the following line
beforeEach(module(function($urlRouterProvider) { $urlRouterProvider.deferIntercept(); }));发布于 2014-12-31 18:45:02
1)您的测试失败了,因为ui-router-extras向app/main/main.html发出了意外的http请求,因此测试失败。
2)事实上,在你所链接的问题上,有很多建议。我假设有额外的调用来加载默认路由的模板,即。otherwise。因此,覆盖它可能会解决问题:
beforeEach(module(function ($urlRouterProvider) {
$urlRouterProvider.otherwise(function(){return false;});
}));https://stackoverflow.com/questions/27724956
复制相似问题