【问题标题】:angularjs factory wrapper around lodash not loaded in testlodash周围的angularjs工厂包装器未在测试中加载
【发布时间】:2013-07-15 22:19:14
【问题描述】:

所以我有以下内容:

angular.module('common.lodash', []).factory('_', function() {
  _.additionFunctionOnLodashThatICreated = function(foo, bar, baz) { ... };
  return _;
});

我在另一个模块中依赖 lodash:

angular.module('common.gridhelper', ['common.lodash']).factory('gridhelper', function(_) {
  var service;
  service.foo = function (collection) {
    // find is a native function on lodash
    return _.find(...);
  }
  return service;
});

当我在浏览器中导航到该站点时,一切都“正常”。但是,当我尝试编写几个测试时,我得到的结果好坏参半。

以下工作,所以我知道我的服务是可访问的:

describe('Lodash Service:', function() {
  beforeEach(module('common.lodash'));
  it('should get an instance of the lodash factory', inject(function(_) {
    expect(_).toBeDefined();
  })); 
});

然而这失败了:

describe('GridHelper Service:', function() {
  var gh;
  beforeEach(function () {
    angular.module('test', [
      'common.lodash',
      'common.gridhelper'
    ]);
  });
  beforeEach(module('test'));

  it('should return a good foo', inject(function(gridhelper) {
    gridhelper.foo({'a': { 'b': 0 }});
  });

返回错误:

TypeError: 'undefined' is not an object (evaluating '_.find(...)')

为了好玩,我尝试在 gridhelper 服务中打印出“_”,通过测试运行时显示为 null,但通过浏览器运行时填充。

有人对我缺少的部分有什么想法吗?

【问题讨论】:

    标签: unit-testing testing angularjs karma-runner


    【解决方案1】:

    事实证明,我的测试在我的实现中暴露了一个问题。 _.find 正在返回 null,我试图从该返回值中提取一些东西而没有先检查。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多