Javascript Karma自动测试许多结构相似的文件

Javascript Karma自动测试许多结构相似的文件,javascript,jasmine,karma-runner,Javascript,Jasmine,Karma Runner,所以我有一个文件夹,里面有很多脚本,都像这样的结构 // Adapter-100.js angular.module('myModule', ['myParentFactory', function(myParentFactory) { return angular.extend(myParentFactory, { "someFunctionA" : function() {}, "someFunctionB" : function() {}, "someFunct

所以我有一个文件夹,里面有很多脚本,都像这样的结构

// Adapter-100.js
angular.module('myModule', ['myParentFactory', function(myParentFactory) {
  return angular.extend(myParentFactory, {
    "someFunctionA" : function() {},
    "someFunctionB" : function() {},
    "someFunctionC" : function() {}
  });
}]);
我的测试只是检查它们是否有这三种方法,问题是大约有100个这样的文件(它们是用于与服务器通信的适配器)

这是我的测试文件的一个表示形式

// api-adapter-tests.js
describe('Unit: EndPointMethods', function() {

  var values, factory, adapter;

  // Boot the module
  beforeEach(function() {
    module('myModule');

    inject(function ($injector) {
      values  = $injector.get('AppConsts');
      factory = $injector.get('EndPointConnection');
      adapter = $injector.get('TestAdapter'); // This needs to change to match what adapter is being tested
    });
  });

  // Run some tests
  describe('AppConsts', function() {
    it('Should have an api_host key', function() {
      expect(values).toBeDefined();
      expect(values.api_host).toBeDefined();
      expect(typeof values.api_host).toBe('string');
    });
  });

  // Is this able to be generated to test each adapter independently?
  describe('TestEndPointMethod has minimum functional definitions', function() {
    it('should have 3 defined functions', function() {
      expect(factory.consumeResponse).toBeDefined();
      expect(factory.getEndPoint).toBeDefined();
      expect(factory.mutator).toBeDefined();
    });
  });
});
我不想为每个适配器编写一个单独的
descripes/it
块,而是让Karma循环遍历所有这些,并动态创建测试(测试不太可能改变)


我在谷歌上搜索了一个解决方案,但似乎找不到任何适合我的解决方案。

你可以用clojure包装你的套件,并通过你想要测试的适配器:摩卡将负责以正确的方式运行它,因此Karma

function runSuiteFor(newAdapter){
  return function(){
    // api-adapter-tests.js
    describe('Unit: EndPointMethods', function() {

      var values, factory, adapter;

      // Boot the module
      beforeEach(function() {
        module('myModule');

        inject(function ($injector) {
          values  = $injector.get('AppConsts');
          factory = $injector.get('EndPointConnection');
          adapter = $injector.get(newAdapter); // set the Adapter here
        });
      });

      // Run some tests
      describe('AppConsts', function() {
        it('Should have an api_host key', function() {
          expect(values).toBeDefined();
          expect(values.api_host).toBeDefined();
          expect(typeof values.api_host).toBe('string');
        });
      });

      // Is this able to be generated to test each adapter independently?
      describe('TestEndPointMethod has minimum functional definitions', function() {
        it('should have 3 defined functions', function() {
          expect(factory.consumeResponse).toBeDefined();
          expect(factory.getEndPoint).toBeDefined();
          expect(factory.mutator).toBeDefined();
        });
      });
    });
    } 
   }

   var adapters = ['MyTestAdapter1', 'MyTestAdapter2', etc...];

   for( var i=0; i<adapters.length; i++){
     runSuiteFor(adapters[i])();
   }
函数runSuiteFor(新适配器){ 返回函数(){ //api-adapter-tests.js 描述('Unit:EndPointMethods',function()){ var值、工厂、适配器; //启动模块 beforeach(函数(){ 模块(“myModule”); 注入(函数($injector){ values=$injector.get('AppConsts'); factory=$injector.get('EndPointConnection'); adapter=$injector.get(newAdapter);//在此处设置适配器 }); }); //运行一些测试 描述('AppConsts',函数(){ 它('应该有一个api_主机密钥',函数(){ expect(values).toBeDefined(); expect(values.api_host).toBeDefined(); expect(typeof values.api_host).toBe('string'); }); }); //是否能够生成此命令以独立测试每个适配器? 描述('TestEndPointMethod具有最小函数定义',函数(){ 它('应该有3个定义的函数',函数(){ expect(factory.consumerresponse).toBeDefined(); expect(factory.getEndPoint).toBeDefined(); expect(factory.mutator).toBeDefined(); }); }); }); } } 变量适配器=['MyTestAdapter1'、'MyTestAdapter2'等];
对于(var i=0;i幸运的是,这段软件只需要支持IE10+:D感谢这一点,我将把它标记为正确答案。