无法解决错误:#<double(anonymous)=“”>收到意外消息:[] = with(:rule_code,1)

时间:2016-07-18 15:01:11

标签: ruby rspec

我正在努力解决上面测试以下错误:

def add_rule_code new_rule
  count = list[:multiple_item_rules].count + list[:total_price_rules].count + 1
  new_rule[:rule_code] = count
end

进行以下测试:

  it "adds a unique rule code" do
    item_rule = double( {rule_type: "item", item_code: 001, number_of_items: 2, new_item_price: 8.50} )
    rules.add_rule_code item_rule
    expect(rules.list[:multiple_item_rules][:rule_code]).to eq 1
  end

我收到以下错误消息:

1) Promotional_Rules New rules adds a unique rule code
   Failure/Error: new_rule[:rule_code] = count
     #<Double (anonymous)> received unexpected message :[]= with (:rule_code, 1)
   # ./lib/Promotional_Rules.rb:21:in `add_rule_code'
   # ./spec/promotional_rules_spec.rb:27:in `block (3 levels) in <top (required)>'

我确定这是一个简单的解决办法,但我无法通过谷歌搜索等方式解决它。任何帮助都会很棒。

由于

1 个答案:

答案 0 :(得分:9)

您正在呼叫new_rule[:rule_code],但双重不响应:[]

一种可以使其发挥作用的方法是:

allow(new_rule).to receive(:[])

请参阅RSpec Mocks Method Stubs

我不知道你为什么要使用双人。我想你可能想用工厂。