Karma自动测试大量类似结构的文件

时间:2014-05-21 14:45:41

标签: javascript jasmine karma-runner

所以我有一个充满脚本的文件夹,它们都像这样的结构

// 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();
    });
  });
});

我不想为每个适配器写一个单独的describes/it块,而是让Karma循环遍历所有这些并立即创建测试(测试不太可能改变)

我已经用Google搜索解决方案,但似乎找不到任何可以为我做这种事情的事情。

1 个答案:

答案 0 :(得分:1)

你可以将你的套件包装成一个clojure并传递你想测试的适配器:mocha将以正确的方式运行它 - 所以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])();
   }

注意:IE8有时会出现这种方法的一些问题,所以万一你会考虑使用Angular 1.2。