Check if the block passed with RSpec Mocks

I can check if the arguments are passed as:

RSpec.describe do it do obj = double expect(obj).to receive(:method).with(1, 2, 3) obj.method(1, 2, 3) end end 

How do I do with a block parameter? My perfect code:

 RSpec.describe do it do obj = double proc = Proc.new{} expect(obj).to receive(:method).with(1, 2, 3).with_block(proc) obj.method(1, 2, 3, &proc) end end 
+7
ruby rspec rspec3
source share
2 answers

It seems I can’t just check if the block with the method chain has passed. And I found one dull answer, Block Implementation:

 RSpec.describe do it do obj = double proc = Proc.new{} expect(obj).to receive(:method).with(1, 2, 3) do |*args, &block| expect(proc).to be(block) end obj.method(1, 2, 3, &proc) end end 

However, we cannot use the implementation of the block and other methods for tuning the response at the same time as receive(:method).with(1, 2, 3){|*| ...}.and_call_original receive(:method).with(1, 2, 3){|*| ...}.and_call_original .

+6
source share

You cannot use wait to verify that a specific block has been transmitted. You can verify that the code is launched by adding code inside it, for example:

 RSpec.describe do it do obj = double block_is = double('block') block = -> { block_is.run } expect(obj).to receive(:method).with(1, 2, 3).and_yield expect(block_is).to receive(:run) obj.method(1, 2, 3, &block) end end 
0
source share

All Articles