Angularjs   发布时间:2022-04-20  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了angularjs – 错误:意外请求:即使在karma / jasmine单元测试中使用html2js,也可以获取嵌套指令的GET views / partials / *大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用Karma和Jasmine对我的 angularjs应用程序进行单元测试.
我有一个指令(比如指令A)模板,其中另一个指令(比如指令B)被渲染,它在应用程序中工作正常,但测试用例无法呈现指令B的模板.
以下是我得到的错误: – @H_673_3@Error: Unexpected request: GET views/partials/directiveb.html Expected GET https://my-sandBox.app.com/123456

以下是指令A的代码: –

@H_673_3@angular.module('myApp') .directive('directiveA',function (myservices,myOtherservices) { return { controller: function(){ /* ... controller function ... */ },templateUrl: '/views/partials/directivea.html',reStrict: 'E',link: function postLink(scope,element,attrs) { /* ...link function... */ } }; });

指令A的模板: –

@H_673_3@<div> <div class="col-md-12"> <h4>We <strong>Need</strong></h4> <div directive-b some-attribute=="true"></div> </div> <div directive-b some-attribute=="false"></div> </div>

指令A的测试案例:

@H_673_3@'use Strict'; describe('Directive: directiveA',function () { // load the directive's module beforeEach(module('myApp')); beforeEach(module('template-module')); var element,appId,reqResponse,scope,dscope,requrl,$httpBACkend,$compile; beforeEach(inject(function ($rootScope,_$httpBACkend_) { scope = $rootScope.$new(); $httpBACkend = _$httpBACkend_; appId = "123456"; requrl = "https://my-sandBox.app.com/" + appId; reqResponse = {} })); it('should Pass',inject(function (_$compile_) { $httpBACkend.expect('GET',requrl).respond(reqResponsE); $compile = _$compile_; element = angular.element('<directive-a/>'); element = $compile(element)(scopE); scope.$digest(); $httpBACkend.flush(); dscope = element.scope(); expect(dscope.myVar).toBe(true); })); });

Karma配置文件: –

@H_673_3@// Karma configuration // http://karma-runner.github.io/0.12/config/configuration-file.html // generator-karma 0.8.2 module.exports = function(config) { config.set({ autoWatch: true,basePath: '../',frameworks: ['jasmine'],preprocessors: { 'app/views/**/*.html': 'html2js' },ngHtml2JsPreprocessor: { StripPrefix: "app",modulename: "template-module" },// list of files / patterns to load in the browser files: [ 'bower_components/angular/angular.js','bower_components/angular-mocks/angular-mocks.js','bower_components/angular-animate/angular-animate.js','bower_components/angular-cookies/angular-cookies.js','bower_components/angular-resource/angular-resource.js','bower_components/angular-route/angular-route.js','bower_components/angular-sanitize/angular-sanitize.js','bower_components/angular-touch/angular-touch.js','bower_components/angular-strap/dist/angular-strap.min.js','bower_components/angular-strap/dist/angular-strap.tpl.min.js','bower_components/ng-file-upload/angular-file-upload-shim.min.js','bower_components/ng-file-upload/angular-file-upload.js','bower_components/jquery/dist/jquery.js','app/scripts/**/*.js','test/mock/**/*.js','test/spec/**/*.js','app/views/**/*.html' ],// list of files / patterns to exclude exclude: ['test/spec/e2e/*'],// web server port port: 8080,browsers: ['PhantomJS'],// Which plugins to enable plugins: [ // 'karma-chrome-launcher','karma-phantomjs-launcher','karma-jasmine','karma-ng-html2js-preprocessor' ],// ConTinuous Integration mode // if true,it capture browsers,run tests and exit singleRun: false,colors: true,// level of logging // possible values: LOG_DISABLE || LOG_ERROR || LOG_WARN || LOG_INFO || LOG_DEBUG logLevel: config.LOG_INFO // Uncomment the following lines if you are using grunt's server to run the tests //proxies: { // '/': 'http://localhost:9000/' // },// URL root prevent conflicts with the site root // urlRoot: '_karma_' }); };

注意:我已经在$templateCache中使用了html2js,但我仍然遇到了这个问题.

指令A的模板URL是/views/partials/directivea.html.这不会导致执行http GET,因为预处理器将模板存储在缓存中: @H_673_3@ngHtml2JsPreprocessor: { StripPrefix: "app",modulename: "template-module" }

但是为views / partials / directiveb.html执行了GET请求.请注意与第一个URL的区别:它没有前导/.模板缓存有一个部分条目,但缓存中的URL是/views/partials/directiveb.html,而不是views / partials / directiveb.html.

确保始终使用绝对路径或相对路径,并根据您的选择,在预处理器配置中去除应用前缀或app /前缀.

大佬总结

以上是大佬教程为你收集整理的angularjs – 错误:意外请求:即使在karma / jasmine单元测试中使用html2js,也可以获取嵌套指令的GET views / partials / *全部内容,希望文章能够帮你解决angularjs – 错误:意外请求:即使在karma / jasmine单元测试中使用html2js,也可以获取嵌套指令的GET views / partials / *所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。