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.
6c933e3af7aa6acaecb58b55d848a98b?d=identicon&s=25 Yun Huang Yong (Guest)
on 2009-03-16 12: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 Yong
yun@nomitor.com                  ...nom nom nom
--
49de4cd2f26705785cbef2b15a9df7aa?d=identicon&s=25 Nick Hoffman (nickh)
on 2009-03-16 16:55
(Received via mailing list)
On Mon, Mar 16, 2009 at 7:00 AM, Yun Huang Yong <yun@nomitor.com> 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
48641c4be1fbe167929fb16c9fd94990?d=identicon&s=25 Mark Wilden (Guest)
on 2009-03-16 17:31
(Received via mailing list)
On Mon, Mar 16, 2009 at 8:33 AM, Nick Hoffman <nick@deadorange.com>
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
Cdf378de2284d8acf137122e541caa28?d=identicon&s=25 Matt Wynne (mattwynne)
on 2009-03-16 20:55
(Received via mailing list)
On 16 Mar 2009, at 11:00, Yun Huang Yong 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 Wynne
http://blog.mattwynne.net
http://www.songkick.com
7217ceaad90d7ae913d8742ccdd697f2?d=identicon&s=25 Guilherme Machado Cirne (Guest)
on 2009-03-22 00:39
(Received via mailing list)
I have this exact same problem. Anyone have a solution?

TIA,

--
Guilherme Machado Cirne
gcirne@gmail.com
48641c4be1fbe167929fb16c9fd94990?d=identicon&s=25 Mark Wilden (Guest)
on 2009-03-22 01:27
(Received via mailing list)
On Mon, Mar 16, 2009 at 4:00 AM, Yun Huang Yong <yun@nomitor.com> 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
D069ac0170dc6c93bad77734258fadae?d=identicon&s=25 Peter Jaros (Guest)
on 2009-03-22 16: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
5d38ab152e1e3e219512a9859fcd93af?d=identicon&s=25 David Chelimsky (Guest)
on 2009-03-22 17:27
(Received via mailing list)
On Sun, Mar 22, 2009 at 10:33 AM, Peter Jaros <peter.a.jaros@gmail.com>
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
48641c4be1fbe167929fb16c9fd94990?d=identicon&s=25 Mark Wilden (Guest)
on 2009-03-22 19:03
(Received via mailing list)
On Mon, Mar 16, 2009 at 4:00 AM, Yun Huang Yong <yun@nomitor.com> 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.