Nicholas Terry
Nicholas Terry

Reputation: 1920

rspec yield block, but call original

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?

Upvotes: 12

Views: 6624

Answers (2)

Nathan Gouy
Nathan Gouy

Reputation: 1412

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

Upvotes: 5

Nicholas Terry
Nicholas Terry

Reputation: 1920

Again, an undocumented feature that i found:

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

le sigh....

Upvotes: 20

Related Questions