angularjs - karma – 带有 templateUrl 的 Angular 单元测试指令失败,即使使用带有 “unexpected request” 的 ng-html2js

标签 angularjs karma-runner ng-html2js

现在我得到:

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)

我正在尝试对一个简单的指令进行单元测试

.directive('Mydirective', function(){
    return {
        restrict:'E',
        replace:true,
        transclude:false,
        templateUrl:'partials/directives/Mydirective .html'

    };
})

这是 karma.conf

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'
        },

这是测试文件。

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%');
          });


      });
    });

这是文件夹树:

-app    
--app/js    
--app/partials    
-config/karma.conf    
-test    
--test/unit/specfile.js

按照不同SO问题中的建议,我已经启用了nghtml2j预处理器并配置了它(希望正确)。但我仍然遇到一个恼人的错误

有什么线索吗?我很高兴对此提供任何帮助

最佳答案

angular.module('st-templates') 与 jasmine 测试中的 module('st-templates') 不同。将 angular.module('st-templates') 更改为 module('st-templates') 它应该可以工作。

关于angularjs - karma – 带有 templateUrl 的 Angular 单元测试指令失败,即使使用带有 “unexpected request” 的 ng-html2js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19700941/

相关文章:

javascript - 使用 AngularJS 过滤表

javascript - 如何仅以特定格式在数组中添加动态键?

gruntjs - grunt.registerTask 中的异步任务

javascript - 测试 JSONP $resource AngularJS

angularjs - 如何在模块配置中模拟提供程序/配置以进行单元测试

angularjs - ngHtml2JsPreprocessor 不加载包含模板的模块,已使用 karma 测试

angularjs - 使用 templateUrl 测试 Angular 指令

Angularjs 选择值未定义

javascript - angularJS 模板 url 不会自动加载

angularjs - Karma 0.12.9,单元测试,karma-ng-html2js-预处理器