Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RSpec - use test double as block argument

Tags:

ruby

rspec

I have some Ruby code which looks like this:

Something.create do |x|
    x.foo = bar
end 

I'd like to write a test which uses a double in place of the block argument x, so that I can then call:

x_double.should_receive(:foo).with("whatever").

Is this possible?

like image 693
stubotnik Avatar asked Sep 28 '12 14:09

stubotnik


1 Answers

 specify 'something' do
   x = double
   x.should_receive(:foo=).with("whatever")
   Something.should_receive(:create).and_yield(x)
   # call the relevant method
 end
like image 129
Mori Avatar answered Nov 13 '22 17:11

Mori