CapnKernul asks:
Hey Avdi. How would you test that a method’s provided block is called
in RSpec? Would you stub #to_proc (for &block) and mock #call?
Typically the way I test that a block is called goes something like this:
describe ZeroWing do let(:probe) { lambda{} } context "given a signal handler" do subject.on_we_get_signal(&probe) it "triggers the handler when signaled" do probe.should_receive(:call) subject.we_get_signal! end end end
That’s for the simple case of just checking if the block is called. When I want to make assertions about the block arguments, I often switch to a style which records the yielded arguments and then makes assertions on them after the fact.
describe Array do subject { [1,2,3] } describe "#reverse_each" do it "yields the elements in backwards order" do yielded = [] subject.reverse_each do |e| yielded << e end yielded.should eq([3,2,1]) end end end
I don’t know if this is the best way, but it’s the way I usually do it.





