Yun Huang Yong
2009-Mar-16 11:00 UTC
[rspec-users] Testing return value of a block argument
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 at nomitor.com ...nom nom nom --
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, > yunHi Yun. Have a look at #and_yield , described at http://rspec.info/documentation/mocks/message_expectations.html . Cheers, Nick
On Mon, Mar 16, 2009 at 8:33 AM, Nick Hoffman <nick at deadorange.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 >> ?} > > Hi Yun. Have a look at #and_yield , described at > http://rspec.info/documentation/mocks/message_expectations.html .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
On 16 Mar 2009, at 11:00, Yun Huang Yong wrote:> I''m using Log4rYou 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
Guilherme Machado Cirne
2009-Mar-21 23:20 UTC
[rspec-users] Testing return value of a block argument
I have this exact same problem. Anyone have a solution? TIA, -- Guilherme Machado Cirne gcirne at gmail.com On Mon, Mar 16, 2009 at 8: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 > > -- > Yun Huang Yong > yun at nomitor.com ...nom nom nom > -- > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20090321/fac25f89/attachment.html>
On Mon, Mar 16, 2009 at 4:00 AM, Yun Huang Yong <yun at 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
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 On Sat, Mar 21, 2009 at 8:25 PM, Mark Wilden <mark at mwilden.com> wrote:> On Mon, Mar 16, 2009 at 4:00 AM, Yun Huang Yong <yun at 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 > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
David Chelimsky
2009-Mar-22 15:59 UTC
[rspec-users] Testing return value of a block argument
On Sun, Mar 22, 2009 at 10:33 AM, Peter Jaros <peter.a.jaros at 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> > Peter > > > On Sat, Mar 21, 2009 at 8:25 PM, Mark Wilden <mark at mwilden.com> wrote: >> On Mon, Mar 16, 2009 at 4:00 AM, Yun Huang Yong <yun at 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 >> _______________________________________________ >> rspec-users mailing list >> rspec-users at rubyforge.org >> http://rubyforge.org/mailman/listinfo/rspec-users >> > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
On Mon, Mar 16, 2009 at 4:00 AM, Yun Huang Yong <yun at 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