Angularjs 为什么$httpBackend.flush()会导致意外请求?
如果添加flush调用,则会出现错误:意外请求:GET templates/createAccountWithAcme.html。它到底为什么告诉我视图模板,我如何避免这种情况?这是一个Ionic Framework V1.0.0 RC2–AngularJS项目 编辑:以前,我错误地忽略了我尝试过的$httpBackend.when或$httpBackend.whenGET调用。更新代码时出现相同错误: 编辑2:更多详细信息:这是一个使用ui路由器的Ionic框架项目v1.0.0 RC2。当我尝试删除所有其他测试时,我仍然得到了错误,但注意到它抱怨的模板是在路由器的UI路由器配置中定义的最后一个模板$stateProvider。如果我在状态定义的末尾添加了一个虚拟状态,那么它就变成了测试抱怨的状态。因此,我决定路由器必须尝试加载我的测试没有预料到的模板。我已经发布了一个补丁Angularjs 为什么$httpBackend.flush()会导致意外请求?,angularjs,ionic-framework,karma-jasmine,Angularjs,Ionic Framework,Karma Jasmine,如果添加flush调用,则会出现错误:意外请求:GET templates/createAccountWithAcme.html。它到底为什么告诉我视图模板,我如何避免这种情况?这是一个Ionic Framework V1.0.0 RC2–AngularJS项目 编辑:以前,我错误地忽略了我尝试过的$httpBackend.when或$httpBackend.whenGET调用。更新代码时出现相同错误: 编辑2:更多详细信息:这是一个使用ui路由器的Ionic框架项目v1.0.0 RC2。当我尝
describe('foo', function() {
var $http, $httpBackend;
$http = void 0;
$httpBackend = void 0;
$http = null;
$httpBackend = null;
beforeEach(module('acme'));
beforeEach(inject(function($injector) {
$http = $injector.get('$http');
$httpBackend = $injector.get('$httpBackend');
$httpBackend.whenGET('http://www.google.com').respond({});
}));
it('should have parsed genres', function() {
console.log('foo');
$http({
method: 'GET',
url: 'http://www.google.com'
}).then(function(response) {
console.log('success: ', response);
}, function(error) {
console.log(error);
});
$httpBackend.flush();
});
});
您需要做的是在beforeEach条款中模拟您的请求:
当您执行.flush时,get方法的承诺将得到解决我最终将所有html模板列为白名单:
beforeEach ...
//this line solves it
$httpBackend.whenGET(/templates/.*/).respond(200, '');
$httpBackend.whenGET('http://www.google.com').respond({});
...
当单元测试在模板中包含其他指令的指令时,我看到了HTML的意外get请求的类似问题 我/我们采用的一般解决方案是添加一些构建步骤,将项目的所有HTML模板“编译”到单个JS文件中,然后将该JS文件包含在karma.conf.JS中的javascript列表中。现在,当指令加载模板并向模板缓存发出请求时,模板将已经加载,因为您的JS是通过karma.conf.JS包含的,并且没有意外的GET请求 请注意,作为构建过程的一部分,这通常是一件好事,这样您的客户端浏览器就不会对HTML模板发出额外的HTTP请求 如果您在基于node.js的项目中,可以使用ng htmljs将所有模板转换为js。他们的文档也能更好地解释这个问题:。这也很方便地被咕噜声和咕噜声包裹着 node.js grunt解决方案的更多细节: Gruntile.coffee:
grunt.initConfig
html2js:
test:
src: ['all/my/html_templates/**/*.html']
dest: 'app/.tmp/templates.js'
options:
base : 'app/'
module: 'myAppTemplates'
singleModule: true
htmlmin:
collapseWhitespace: false
collapseBooleanAttributes: true
removeCommentsFromCDATA: true
removeOptionalTags: true
grunt.registerTask 'test', [
...
'html2js:test'
'karma:unit'
...
]
在Karma.conf.js中:
files: [
...
'app/.tmp/templates.js'
],
不要忘记在测试中包含模板模块:
beforeEach module 'myAppTemplates'
#or if you are using browserify:
beforeEach window.angular.mock.module 'myAppTemplates'
谢谢,不知怎的,我不小心离开了我的。当“出去”的时候,我也试了一下whenGET。我用正确的代码编辑了我的问题,但仍然得到相同的错误。这是唯一正在运行的测试吗?acme模块是否有正在执行的运行块?@Chandermani它不是唯一正在运行的测试,但它是唯一失败的测试。如果我将flush调用移动到另一个测试中,它也会失败。到目前为止,这是我唯一想去的地方。在我的应用程序中,acme模块确实有一个运行块,是的,尽管我是karma/jasmine的新手,不知道在运行测试时是否会执行该运行块。我不这么认为,但我是在假设。可能在获取模板的路径上有一些测试,或者有一些加载指令模板的指令测试。请参阅上述模板的代码参考,并查看是否有相应的测试。当/templates/*/出现语法错误时。如果有机会,请查看它?@scniro很晚了,但应该是whenGET/templates\/.*/-转义regexp特殊符号
beforeEach module 'myAppTemplates'
#or if you are using browserify:
beforeEach window.angular.mock.module 'myAppTemplates'