[rspec-users] Message expectation that verifies result of passed block

Paul Hinze paul.t.hinze at gmail.com
Thu Jan 28 08:29:49 EST 2010


Hey speclers,

My spec-fu is failing me on a message expectation in which I would like to
verify that the block passed to a certain method yields the proper
value.  I would like to be able to say something like:

def bar
  # .. some code
  foo do
    'bar' # want to verify this value
  end
end

describe 'bar' do
  it 'calls foo with proper block' do
    should_receive(:foo).with(block_yielding('bar'))
  end
end

The problem I've been banging my head against is the fact that I can't
seem to capture the passed block using this notation:

  should_receive(:foo) { |*args, &block|
    yield.should == 'bar'      # breaks
    block.call.should == 'bar' # no dice
  }

I believe the lack of ability to use this notation comes down to a ruby
limitation, but I'm not sure.  If that's the case, then we would need a
specific argument expectation (along the lines of my suggestion) that
executes in a context in which it can call the block.

Am I missing something obvious here, or does rspec currently not allow
for this easily?  If not, let's discuss how it should look and I can
work on adding feature.

Cheers,

Paul


More information about the rspec-users mailing list