[rspec-users] Asserting on a yield

Morten Møller Riis mortenmoellerriis at gmail.com
Thu Mar 8 11:57:05 UTC 2012


&:self won't work because self isn't an instance method on the object, eg. some_object.self won't work either.

If you wan't something cleaner couldn't you use this method in spec_helper.rb ?

def block_yield(object, method)
  object.send(method) { yield if block_given? }
end

Then:

block_yield(thing, :do_stuff).should == "value"



Mvh
Morten Møller Riis



On Mar 7, 2012, at 9:42 PM, David Chelimsky wrote:

> On Wed, Mar 7, 2012 at 2:32 PM, Matt Wynne <matt at mattwynne.net> wrote:
>> 
>> On 7 Mar 2012, at 18:16, David Chelimsky wrote:
>> 
>> On Wed, Mar 7, 2012 at 11:56 AM, Matt Wynne <matt at mattwynne.net> wrote:
>> 
>> 
>> On 7 Mar 2012, at 11:39, Morten Møller Riis wrote:
>> 
>> 
>> On Mar 7, 2012, at 8:22 AM, Matt Wynne wrote:
>> 
>> 
>> 
>> Hi all,
>> 
>> 
>> I'm spec'ing a method that yields a value. Right now, I spec it like this:
>> 
>> 
>>     result = nil
>> 
>>     thing.do_stuff { |value| result = value }
>> 
>>     result.should == expected
>> 
>> 
>> This feels like too much ceremony. What I want to do is something more this:
>> 
>> 
>>    thing.do_stuff.should yield_value(expected)
>> 
>> 
>> Is there anything built into RSpec to let me do this? If not, how do other
>> 
>> people test yields?
>> 
>> 
>> cheers,
>> 
>> Matt
>> 
>> 
>> 
>> How about this?
>> 
>> 
>> thing.do_stuff(&:to_s).should == expected
>> 
>> 
>> 
>> Yes, that's a neat hack, but I'd prefer to be able to assert on the actual
>> 
>> yielded value, instead of the result of calling an arbitrary method on it.
>> 
>> 
>> thing.do_stuff(&:self) would be a bit less arbitrary :)
>> 
>> 
>> It would, but does it work?
>> 
>> I assumed it would too but here on my Ruby 1.9.2 it gives me a NoMethodError :(
> 
> I had assumed as well, and you know what they say about ASSuming!
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users



More information about the rspec-users mailing list