9

So I have the following:

foo.each do |f|
  f.begin
    do_stuff
    do_more_stuff
  end
end

And I mock the f object with an and_yield() call. I want to be able to test the begin method by passing it the original block { do_stuff do_more_stuff }, not a mock implementation.... I cant just let the begin method be called on the mock without at least stubbing it, so what do I do?

Nicholas Terry
  • 1,812
  • 24
  • 40

2 Answers2

19

Again, an undocumented feature that i found:

allow(thing).to receive(:foo) do |_, &block|
  block.call
end

le sigh....

Nicholas Terry
  • 1,812
  • 24
  • 40
3

The following worked for me:

original = thing.method(:foo)
expect(thing).to receive(:foo) do |_params|
  # check params
  expect(params).to include(something)

  # then
  original.call(params)
end
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Nathan Gouy
  • 1,072
  • 9
  • 19