【问题标题】:Issue with spies in RspecRspec 中的间谍问题
【发布时间】:2018-01-03 20:57:57
【问题描述】:

无法弄清楚原因:

it "shifts/unshifts without O(n) copying" do
  arr = RingBuffer.new

  allow(arr.send(:store)).to receive(:[]=).and_call_original
  8.times do |i|
    arr.unshift(i)
  end

  # Should involve 8 sets to unshift, no more.
  expect(arr.send(:store)).to have_received(:[]=).exactly(8).times
end

结果:

“失败/错误:expect(arr.store).to have_received(:[]=).exactly(8).times # 预计会收到 []=,但该对象不是间谍或方法尚未被存根。”

【问题讨论】:

    标签: ruby rspec rspec-mocks


    【解决方案1】:

    不完全确定您的代码在做什么,但我怀疑调用 arr.send(:store) 每次都会返回不同的对象。尝试这样修改:

    it "shifts/unshifts without O(n) copying" do
      arr = RingBuffer.new
      store = arr.send(:store)
    
      allow(store).to receive(:[]=).and_call_original
      8.times do |i|
        arr.unshift(i)
      end
    
      # Should involve 8 sets to unshift, no more.
      expect(store).to have_received(:[]=).exactly(8).times
    end
    

    【讨论】:

    • 我支持你。
    猜你喜欢
    • 2015-09-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多