UI路由器额外打破我的单元测试有意想不到的效果的错误? [英] UI Router Extras breaks my unit tests with unexpected results error?
问题描述
问:
- 为什么我的测试中失败时,UI的路由器演员(不正常的UI路由器)是安装
? - 我如何使用 UI的路由器额外
,仍然有我的测试都通过了
的如果你想安装此快速使用自耕农+角fullstack发电机+凉亭安装UI的路由器额外的
我发现与正常UI路由器类似的问题。
- Luckially,UI路由器正常工作只是我的测试罚款。
- 路由器-额外我得到一个错误后
如果我卸载UI的router.extras它这个测试通过就好了:
更新$ urlRouterProvider测试beforeEach模块
继承人我的测试:
使用严格的;描述(控制器:MainCtrl',函数(){ //加载控制器模块
beforeEach(模块('morningharwoodApp'));
beforeEach(模块('socketMock')); VAR MainCtrl,
范围,
$ httpBackend; //初始化控制器和模拟范围
beforeEach(
注(功能(_ $ httpBackend_,$控制器,$ rootScope){
$ httpBackend = _ $ httpBackend_;
$ httpBackend.expectGET('/ API /事物)
.respond(['HTML5样板','AngularJS','因果报应','前preSS']); 范围= $ rootScope $新的()。
MainCtrl = $控制器('MainCtrl',{
$适用范围:适用范围
});
}),
模块(函数($ urlRouterProvider){
$ urlRouterProvider.otherwise(函数(){返回false;});
})
); 它('应该重视的东西范围的名单,函数(){
$ httpBackend.flush();
期待(scope.awesomeThings.length).toBe(4);
});
});
下面是我的 karma.conf
module.exports =功能(配置){
config.set({
//基本路径,将用于解析文件和排除
基本路径:'', //测试框架使用(茉莉/摩卡/ qunit / ...)
框架:['茉莉'], //的文件/目录模式在浏览器中加载
文件:
客户端/ bower_components / jQuery的/距离/ jquery.js和'
客户端/ bower_components /角/ angular.js',
客户端/ bower_components /角嘲笑/角mocks.js',
客户端/ bower_components /角资源/角resource.js',
客户端/ bower_components /角饼干/角cookies.js',
客户端/ bower_components /角的sanitize /角sanitize.js',
客户端/ bower_components / lodash /距离/ lodash.compat.js',
客户端/ bower_components /角座-IO / socket.js',
客户端/ bower_components /角度的UI路由器/发行/角-UI-router.js',
客户端/ bower_components /著名-polyfills / classList.js',
客户端/ bower_components /著名-polyfills / functionPrototypeBind.js',
客户端/ bower_components /著名-polyfills / requestAnimationFrame.js',
客户端/ bower_components /有名/距离/著名-global.js,
客户端/ bower_components /有名的角/距离/著名-angular.js',
客户端/应用/ app.js,
客户端/应用/ app.coffee',
客户端/应用/ ** / *。JS',
客户端/应用/ ** / *。咖啡,
客户端/组件/ ** / *。JS',
客户端/组件/ ** / *。咖啡,
客户端/应用/ ** / *。玉',
客户端/组件/ ** / *。玉',
客户端/应用/ ** / *。HTML,
客户端/组件/ ** / *。HTML
] preprocessors:{
** / *玉':'NG-jade2js',
** / * HTML。':'html2js',
** / *咖啡:咖啡,
}, ngHtml2Js preprocessor:{
带preFIX:客户端/
}, ngJade2Js preprocessor:{
带preFIX:客户端/
}, //的文件/模式列表中排除
排除:[], // Web服务器端口
端口:8080, //日志记录级别
//可能的值:LOG_DISABLE || LOG_ERROR || LOG_WARN || LOG_INFO || LOG_DEBUG
LOGLEVEL:config.LOG_INFO,
//启用/禁用看文件和执行测试,只要任何文件更改
autoWatch:假的,
//启动这些浏览器,目前市面上:
// - 镀铬
// - ChromeCanary
// - 火狐
// - 歌剧
// - 野生动物园(仅限Mac)
// - PhantomJS
// - IE(仅限于Windows)
浏览器:['PhantomJS'],
//持续集成模式
//如果是真的,它捕获的浏览器,运行测试和退出
singleRun:假的
});
};
这可能是具有对 $状态
依赖某个组件的结果在这种情况下 $状态
将被实例化和默认路由将被执行。这就是为什么你的控制器之一的模板 main.html中
正在牵强。
要绕过这个,替换去()
和 transitionTo()
的 $状态
与假人方式:
beforeEach(注(功能(_ $ STATE_){
状态= _ $ STATE_;
spyOn(州,走出去);
spyOn(州,'transitionTo');
}));
QUESTION:
- Why are my tests failing when ui-router-extras (not normal ui-router) is install?
- How can I use ui-router-extras
and still have my tests pass?
If you want to install this quickly use yeoman + angular-fullstack-generator + bower install ui-router-extras
I found a similar issue with normal ui-router.
- Luckially, ui-router normal works just fine with my testing.
- After installing ui-router-extras I get an ERROR
If I uninstall ui-router.extras it this test passes just fine:
UPDATED for beforeEach module of $urlRouterProvider TEST Heres my test:
'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);
});
});
Here's my 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
});
};
This might be a result of some component having a dependency on $state
in which case $state
will be instantiated and default route will be executed. This is why a template of one of your controllers main.html
is being fetched.
To bypass this, replace go()
and transitionTo()
of $state
methods with dummies:
beforeEach( inject( function ( _$state_ ) {
state = _$state_;
spyOn( state, 'go' );
spyOn( state, 'transitionTo' );
} ) );
这篇关于UI路由器额外打破我的单元测试有意想不到的效果的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!