首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >即使对于带有“意外请求”的ng-html2js,带有templateUrl的karma角单元测试指令也会失败。

即使对于带有“意外请求”的ng-html2js,带有templateUrl的karma角单元测试指令也会失败。
EN

Stack Overflow用户
提问于 2013-10-31 07:52:00
回答 1查看 3.5K关注 0票数 2

现在我得到了:

代码语言:javascript
复制
INFO [karma]: Karma v0.10.2 server started at `http://localhost:9876/`
INFO [launcher]: Starting browser Chrome
INFO [Chrome 30.0.1599(Linux)]: Connected on socket Q8d9RLBQDqi7wJ8iaKNw Chrome 30.0.1599 (Linux)
directives Mydirective  directive should render Roi element  correctly FAILED
Error: Unexpected request: GET partials/directives/Mydirective.html
No more request expected
    at Error (<anonymous>)
    at $httpBackend (/home/user/Documents/Projects/angularproject/test/lib/angular/angular-mocks.js:1006:9)

我正在尝试统一一个简单的指令

代码语言:javascript
复制
.directive('Mydirective', function(){
    return {
        restrict:'E',
        replace:true,
        transclude:false,
        templateUrl:'partials/directives/Mydirective .html'

    };
})

我是karma.conf

代码语言:javascript
复制
module.exports = function(config){
    config.set({
    basePath : '../',

    files : [
        //libs, not sure I need all of them
        'app/lib/angular/angular.js',
        'app/lib/angular-file-upload/**/*.js',
        'app/lib/restangular/restangular.js',
        'app/lib/jquery/jquery.js',
        'app/lib/angular/**/*.js',
        'app/lib/lodash/**/*.js',
        'app/lib/Underscore.js',
        'app/lib/bootstrap.min.js',
        'app/lib/ui-bootstrap-0.6.0.js',
        'app/lib/angular/angular-resource.js',
        'app/lib/angular/angular-cookies.min.js',
        'app/lib/angular-translate/**/*.js',
        'app/lib/angular-translate-loader-static-files/**/*.js',
        'app/lib/angular-translate-storage-cookie/**/*.js',
        'app/lib/angulartestfile-translate-storage-local/**/*.js',
        'app/lib/bootstrap-gh-pages/assets/rainbow.js',
        'app/lib/fbAngular.js',
        'app/lib/moment.js',
        // test libs
        'test/lib/angular/**/*.js',
        'test/lib/**/*.js',
        // my js to test
        'app/js/**/*.js',
        //test unit
        'test/unit/**/*.js',

        //directive templates
        'app/partials/directives/**/*.html'
    ],
    exclude: ['test/lib/angular/angular-scenario.js'],

    autoWatch : true,

testfile

    frameworks: ['jasmine'],

    browsers : ['Chrome'],


    plugins : [
            'karma-junit-reporter',
            'karma-chrome-launcher',
            'karma-firehttp://localhost:9876/fox-launcher',
            'karma-jasmine',
            'karma-coverage',
            //'karma-html2js-preprocessor',
            'karma-ng-html2js-preprocessor'
            ],

    junitReporter : {
      outputFile: 'test_out/unit.xml',
      suite: 'unit'
    },

        reporters:['coverage','progress'],


        preprocessors : {
        '**/js/*.js': 'coverage',
        '**/partials/directives/*.html': 'ng-html2js'// generate js files from html templates

        },
        ngHtml2JsPreprotestfilecessor: {
//            cacheIdFromPath : function(filepath) {
//                return filepath.substr(filepath.indexOf("angular")+8);
//            },
         stripPrefix:'app/',
                // setting this option will create only a single module that contains templates
                // from all the files, so you can load them all with module('templates')
         moduleName: 'st-templates'
        },

这是测试文件。

代码语言:javascript
复制
describe('directives', function() {
    var $compile, $rootScope;
  beforeEach(function(){
      module('myModule.directives');
      module(function($provide) {
          $provide.value('version', 'TEST_VER');
          $provide.value('somevalue','value');
      }
      angular.module('st-templates');


  });

    beforeEach(inject(function(_$compile_, _$rootScope_){
        // The injector unwraps the underscores (_) from around the parameter names when matching
        $compile = _$compile_;
        $rootScope = _$rootScope_;

    }));

    describe('app-version directive', function() {
    it('should print current version', function() {
//      module(function($provide) {
//        $provide.value('version', 'TEST_VER');
        var element = $compile('<span app-version></span>')($rootScope);
        $rootScope.$digest();
        expect(element.text()).toEqual('TEST_VER');

    });


    });


    describe('mydirective', function(){

      it('should render Roi element correctly', function(){
              var element = $compile('<st-roi></st-roi>')($rootScope);
              $rootScope.$digest();
              expect(element.text()).toEqual('0.3%');
          });


      });
    });

这是文件夹树:

代码语言:javascript
复制
-app    
--app/js    
--app/partials    
-config/karma.conf    
-test    
--test/unit/specfile.js

按照不同的建议,我已经启用了nghtml2j预处理器并配置了它(希望是正确的)。但我还是有个恼人的错误

有什么线索吗?我很乐意在这方面提供任何帮助

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-11-01 21:38:25

angular.module('st-templates')与茉莉花测试中的module('st-templates')不一样。将angular.module('st-templates')更改为module('st-templates'),它应该可以工作。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19700941

复制
相关文章

相似问题

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