0

当我在 rails 中使用 mongoid mapreduce 时,我通过字符串传递 map 和 reduce 函数。例如

def group
  Order.collection.map_reduce(map, reduce, :out => 'output_collection')
end

def map
  "function() 
   {
      var key = {game_id: this.game_id, year: this.date.getFullYear()};
      emit(key, {count: 1, score: this.score}); 
   }"
end

def reduce
  "function() 
   {
     var result = {sum: 0, total_score: 0, average: 0};
     values.forEach(function(value)
     {
       result.sum += value.count;
       result.total_score += value.score;
     });
   }"
end

是否可以在 Rails 中测试 map 和 reduce 功能。这是一个简单的例子。但是我的项目功能比较复杂,我觉得很难维护。

感谢您对任何建议的帮助。

4

1 回答 1

0

对于在数据库中进行的功能测试,您的测试方法是:

  1. 创建基本数据集
  2. 运行函数
  3. 将结果与您的预期进行比较

因此,在这种情况下,您需要确保 sum/average/total_score 符合您的预期。

于 2012-06-28T14:54:38.803 回答