如何为更新哈希散列的方法编写测试

时间:2016-07-19 15:08:55

标签: ruby rspec

我正在为测试以下方法的方法编写测试:

  def update rule_code, updated_rule
    list.select{ |rule|
      if rule[0] == rule_code
         rule[1] = updated_rule
       end
     }
  end

测试看起来像:

  describe "#update" do
    it "updates a rule when given the rulecode and rule updates" do
      item_rule = double( {rule_type: "item", item_code: 1, number_of_items: 2, new_item_price: 8.50} )
      updated_rule = double( {rule_type: "item", item_code: 1, number_of_items: 4, new_item_price: 7.50} )
      rule_code = 1
      rules.add item_rule
      expect{rules.update(rule_code, updated_rule)}.to change{rules.list[rule_code]}.from(item_rule).to(updated_rule)
    end
  end

我收到以下错误:

1) Promotional_Rules#update updates a rule when given the rulecode and rule updates
   Failure/Error: rule[1] = updated_rule

   NoMethodError:
     undefined method `[]=' for 1:Fixnum
   # ./lib/Promotional_Rules.rb:21:in `block in update'
   # ./lib/Promotional_Rules.rb:19:in `select'
   # ./lib/Promotional_Rules.rb:19:in `update'
   # ./spec/promotional_rules_spec.rb:39:in `block (4 levels) in <top (required)>'
   # ./spec/promotional_rules_spec.rb:39:in `block (3 levels) in <top (required)>'

测试结果不是问题,因为我确信我的测试的最后一行是不正确的。我试图在下面的哈希中测试它:

list = {1: {rule_type: "item", item_code: 1, number_of_items: 2, new_item_price: 8.50}, 2: {rule_type: "item", item_code: 2, number_of_items: 6, new_item_price: 9.50}

如果我搜索原始密钥&#34; 1&#34;然后我可以用相应的对更新它。我也意识到我的方法也不正确,因此测试失败。

0 个答案:

没有答案