Gulp角度单位测试指令templateUrl

时间:2015-02-26 16:28:50

标签: angularjs unit-testing angularjs-directive gulp browserify

我在网上阅读了很多帖子,但是找不到任何有效的解决方案 我正在尝试为指令编写单元测试,并希望从模板缓存

提供html文件

我正在使用Gulp作为构建工具

测试的gulp任务看起来像这样

gulp.task('test', function (done) {
    karma.start({
        configFile:__dirname + '/' + config.test.karmaconf,
        singleRun: false
    }, done);
});

并且在karma.conf中,我已经定义了

    browserify: {
        debug: true,
        transform: [['browserify-ng-html2js', {
            module: 'templates',
            extension: 'html'
        }]]
    } 

在单元测试中,我声明了

beforeEach(angular.mock.module('templates'));

但接收

Error: Unexpected request: GET path/to/some.html
No more request expected
    at $httpBackend 

我还尝试在karma.conf中使用preprocessorsngHtml2JsPreprocessor但没有成功。任何人都可以提供解决方案或指出我如何调试为什么不提供模板?

1 个答案:

答案 0 :(得分:3)

解决方案

你可以通过多种方式做到这一点。以下是我的表现方式:

  1. 使用gulp-angular-templatecache
  2. gulp.task('bundle-common', function() {
      return merge(
        // Store all html files in $templateCache
        gulp.src([
          appDir + '/common/**/*.html'
        ])
        .pipe($$.angularTemplatecache({
          root: 'common'
        }))
    
        // Append everything else: directives, tests, etc.
        gulp.src([
          appDir + '/common/**/*.js'
        ])
      )
      .pipe($$.concat('common.js'))
      .pipe(gulp.dest('public/js/'));
    });
    

    这将输出一个名为common.js的文件,其中包含所有html模板和指令。

    看起来有点像这样:

    angular.module("templates").run(["$templateCache", function($templateCache) {
    $templateCache.put("common/a-great-eye.html","<div>lidless, wreathed in flame, 2 times</div>");}]);
    
    angular.module("common")
    
    .directive('aGreatEye', function () {
        return {
            restrict: 'E',
            replace: true,
            templateUrl: 'common/a-great-eye.html'
        };
    });
    
    ...
    
    1. karma.conf.js加载common.js
    2.      files: [
            '../vendor/jquery/dist/jquery.js',
            '../vendor/angular/angular.js',
            '../vendor/angular-mocks/angular-mocks.js',
            '../public/js/common.js'
          ]
      
      1. 在测试文件中引用templates模块:
      2. describe('Unit testing great quotes', function() {
          var $compile,
              $rootScope;
        
          // Load the common module, which contains the directive
          beforeEach(function() {
            module('templates');
            module('common');
          });
        
          // Store references to $rootScope and $compile
          // so they are available to all tests in this describe block
          beforeEach(inject(function(_$compile_, _$rootScope_){
            // The injector unwraps the underscores (_) from around the parameter names when matching
            $compile = _$compile_;
            $rootScope = _$rootScope_;
          }));
        
          it('Replaces the element with the appropriate content', function() {
            // Compile a piece of HTML containing the directive
            var element = $compile("<a-great-eye></a-great-eye>")($rootScope);
            // fire all the watches, so the scope expression {{1 + 1}} will be evaluated
            $rootScope.$digest();
            // Check that the compiled element contains the templated content
            expect(element.html()).toContain("lidless, wreathed in flame, 2 times");
          });
        });
        

        调试

        在Chrome浏览器中开始测试,然后点击“调试”按钮。然后打开开发者控制台。

        几点提示:

        • 您可以在测试中添加断点。
        • 确保控制台窗口中没有错误,尤其是注入错误。如果这样做,请验证您在karma.config.js中请求的所有js文件(它们将在Developer Console中列出)是否正确加载并包含您需要的代码(角度,模板,指令等)。
相关问题