首页 文章

Jasmine单元测试模块未定义

提问于
浏览
0

我目前正忙于测试Angular的茉莉花单元 . 我把这个单元测试放在test.js中

describe('The countInput filter', function(){
    var $filter;

    beforeEach(function(){
        module('countInputFilter');

        inject(function(_$filter_){
            $filter = _$filter_;
        });
    });

    it('Should not give an output when the length of the input is > 3', function(){

        var input = 'test';

        result = $filter('countInputFilter')(input);

        expect(result).toBe(null);
    });


});

当我用karma启动karma.conf.js时,我得到错误:模块未定义 .

在我的karma.conf.js文件中,我有以下文件代码:

files: [
  '../bower_components/angular/angular.js',
  '../bower_components/angular-mocks.js',
  '../bower_components/angular-route/angular-route.js',
  '../scripts/**/*.js',
  '../scripts/filters.js',
  'spec/**/*.js',
  'spec/*.js'
],

我的项目结构是:

scripts
--filters.js
test
--spec
----test.js
--karma.conf.js

第一次尝试使用Jasmine进行单元测试,但我没有看到错误 .

26 03 2017 11:29:10.103:WARN [观察者]:模式"C:/Users/user/Desktop/angular/bower_components/angular-mocks.js"与任何文件都不匹配 . 26 03 2017 11:29:10.134:警告[业力]:没有捕获的浏览器,打开http://localhost:9876/ 26 03 2017 11:29:10.150:INFO [业力]:业力v1.5.0服务器开始于http://0.0.0.0:9876/ 26 03 2017 11:29:10.150 :INFO [launcher]:启动具有无限并发性的浏览器Chrome 26 03 2017 11:29:10.181:INFO [launcher]:启动浏览器Chrome

1 回答

  • 1

    检查模块名称 .

    模块名称和过滤器名称不能相同 .

    I.e module('countInputFilter');

    result = $filter('countInputFilter')(input);

    如果 filter.js 中的模块名称为 countInput 且filtername为 countInputFilter

    试试以下代码:

    describe('The countInput filter', function(){
      var $filter;
    
      beforeEach(function(){
        module('countInput');
    
        inject(function(_$filter_){
            $filter = _$filter_;
        });
      });
    
      it('Should not give an output when the length of the input is > 3', function(){
    
        var input = 'test';
    
        result = $filter('countInputFilter')(input);
    
        expect(result).toBe(null);
      });
    
    
    });
    

    filter.js 也应该是这样的:

    angular.module('countInput', [])
      .filter('countInputFilter', function () {
        return function (x) {
            ...
        };
    });
    

    你也可以分享你的 filter.js 代码吗?

    Here is the sample jsfiddle link for unit testing filter

相关问题