[rspec-users] Testing return value of a block argument

Nick Hoffman nick at deadorange.com
Mon Mar 16 11:33:28 EDT 2009


On Mon, Mar 16, 2009 at 7:00 AM, Yun Huang Yong <yun at nomitor.com> wrote:
> Hi,
>
> I'm using Log4r and one of its neat features is its handling of blocks such
> that you can do:
>  log.debug { "Something bad happened" + some_expensive_method() }
> instead of:
>  log.debug("Something bad happened" + some_expensive_method())
>
> The benefit of the former is that some_expensive_method() is not executed
> until the debug() method executes so if your log level doesn't include DEBUG
> you don't suffer the performance hit of calling some_expensive_method()
> needlessly.
>
> Is there any way to test what the resultant log message is from evaluating
> the block?
>
> i.e. something like:
>  log_mock.should_receive(:debug) { |log_block|
>    # execute log_block, and check its return value
>  }
>
> I have a simple bit of code to tinker with: http://gist.github.com/79820
>
> Thanks in advance,
> yun

Hi Yun. Have a look at #and_yield , described at
http://rspec.info/documentation/mocks/message_expectations.html .

Cheers,
Nick


More information about the rspec-users mailing list