因果报应 - 一个templateUrl角单元测试指令失败,即使NG-html2js用“意外的请求”因果报应、指令、单元测试、意外

2023-09-13 02:45:09 作者:暴雨唤醒夏天

现在我越来越:

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)

我想单元测试一个简单的指令

I'm trying to unittest a simple directives

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

    };
})

这是karma.conf

this is 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

继在不同的,所以问题的建议,我已经启用了nghtml2j preprocessor和(正确的希望)配置它。但我仍然得到一个恼人的错误

Following advice in different SO question I already enabled the nghtml2j preprocessor and configured it (Hopefully correctly). But I still get an annoying error

任何线索?我会很高兴与此任何帮助。

Any clues? I'll be glad for any help with this

推荐答案

angular.module(ST-模板')是不一样的模块(ST-模板')的茉莉测试中。更改 angular.module(ST-模板')模块(ST-模板'),它应该工作

angular.module('st-templates') is not the same as module('st-templates') inside that jasmine test. Change angular.module('st-templates') to module('st-templates') and it should work.