Forum: RSpec Testing return value of a block argument

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Yun Huang Y. (Guest)
on 2009-03-16 13:03
(Received via mailing list)
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

--
Yun Huang Y.
removed_email_address@domain.invalid                  ...nom nom nom
--
Nick H. (Guest)
on 2009-03-16 17:55
(Received via mailing list)
On Mon, Mar 16, 2009 at 7:00 AM, Yun Huang Y. 
<removed_email_address@domain.invalid> wrote:
> you don't suffer the performance hit of calling some_expensive_method()
> 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_expe... .

Cheers,
Nick
Mark W. (Guest)
on 2009-03-16 18:31
(Received via mailing list)
On Mon, Mar 16, 2009 at 8:33 AM, Nick H. <removed_email_address@domain.invalid>
wrote:
> http://rspec.info/documentation/mocks/message_expe... .
That was my first thought, but and_yield stubs what the method will
yield, not what it should yield. It's like and_return.

///ark
Matt W. (Guest)
on 2009-03-16 21:55
(Received via mailing list)
On 16 Mar 2009, at 11:00, Yun Huang Y. wrote:

> I'm using Log4r

You might want to check out this library, which seems to be better
maintained these days:

http://github.com/TwP/logging/tree/master

Matt W.
http://blog.mattwynne.net
http://www.songkick.com
Guilherme Machado C. (Guest)
on 2009-03-22 01:39
(Received via mailing list)
I have this exact same problem. Anyone have a solution?

TIA,

--
Guilherme Machado C.
removed_email_address@domain.invalid
Mark W. (Guest)
on 2009-03-22 02:27
(Received via mailing list)
On Mon, Mar 16, 2009 at 4:00 AM, Yun Huang Y. 
<removed_email_address@domain.invalid> wrote:
>
> i.e. something like:
>  log_mock.should_receive(:debug) { |log_block|
>    # execute log_block, and check its return value
>  }

I suppose you could always look at the source code for the debug
method, find out what it does with the result of the block and set an
expectation on that behavior.

///ark
Peter J. (Guest)
on 2009-03-22 17:52
(Received via mailing list)
Going forward, what do people think about being able to say

mock.should_receive(:debug).with_block  { |log_block|
  # execute log_block, and check its return value
 }

Thoughts?

Peter
David C. (Guest)
on 2009-03-22 18:27
(Received via mailing list)
On Sun, Mar 22, 2009 at 10:33 AM, Peter J. 
<removed_email_address@domain.invalid>
wrote:
> Going forward, what do people think about being able to say
>
> mock.should_receive(:debug).with_block  { |log_block|
>  # execute log_block, and check its return value
>  }
>
> Thoughts?

I like the idea, but I don't know how well it will play with other
sorts of expectations. We'd probably need to alias it with and_block
so you could say:

mock.should_receive(:message).with(:these, "args").and_block {|block|
.... }

Would you please file a feature request for this?

Thanks,
David
Mark W. (Guest)
on 2009-03-22 20:03
(Received via mailing list)
On Mon, Mar 16, 2009 at 4:00 AM, Yun Huang Y. 
<removed_email_address@domain.invalid> wrote:
>
> 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
>  }

Sorry - I don't quite understand. Do you want to test the logger debug
method's behavior when you pass it a certain block, or do you want to
test what the block itself does? If the former, it seems to me that
you wouldn't want to mock the logger object. If the latter, you can
just test the block directly.

///ark
This topic is locked and can not be replied to.