[rspec-devel] [ rspec-Feature Requests-11949 ] & amp; quot; should_yield& amp; quot; or & amp; quot; .should raise_error(MySpecificError)& amp; quot; equivalent for yields

noreply at rubyforge.org noreply at rubyforge.org
Mon Nov 19 00:08:13 EST 2007


Feature Requests item #11949, was opened at 2007-07-01 20:49
You can respond by visiting: 
http://rubyforge.org/tracker/?func=detail&atid=3152&aid=11949&group_id=797

Category: expectation module
Group: None
Status: Open
Priority: 3
Submitted By: S. Potter (mbbx6spp)
Assigned to: Nobody (None)
Summary: "should_yield" or ".should raise_error(MySpecificError)" equivalent for yields

Initial Comment:
The problem is already described here:
http://snakesgemscoffee.blogspot.com/2007/07/rspec-shouldyield.html

In summary:
Just like we can write:
  lambda { Client.configure(&spec_block(1)) }.should raise_error(MySpecificError)
We should also be able to write:
  lambda { Client.configure(&spec_block(1)) }.should_yield(Config)
This should mean, to the "spec_block" that expects one (1) argument of type Config.

Other suggestions for syntax are:
  lambda { Client.configure(&spec_block(1)) }.should yield_instances_of(Config)
  lambda { Client.configure(&spec_block(1)) }.should yield_objects_of(Config)
Or something else similar (these are just things that popped in my head right now.

Feedback much appreciated.

----------------------------------------------------------------------

Comment By: Chad Humphries (spicycode)
Date: 2007-11-19 00:08

Message:
Moved to http://rspec.lighthouseapp.com/projects/5645-rspec/tickets/100-11949-amp-quot-should_yield-amp-quot-or-amp-quot-should-raise_error-myspecificerror-amp-quot-equivalent-for-yields#ticket-100-2

----------------------------------------------------------------------

Comment By: S. Potter (mbbx6spp)
Date: 2007-07-09 18:59

Message:
Aslak, that is very similar (with less validation) to what I
already do (as explained in the blog entry above), but
doesn't add too much value if we are following a DSL
approach in my opinion.

My thoughts on what spec_block would do is generate a block
with whatever arity is given for it.  For example, if I call
spec_block(3) that block will produce a block that has 3
arguments that cannot be ignored.  I am open to suggestions
on the naming of this helper method, but I would prefer that
the specs I write for my application don't require so much
setup to specify such a simple thing.

Those are just my thoughts and will leave this open until
there is a bit more clarity to what the RSpec developer
consensus is.  I will work on a patch based on the vision
after that point.

----------------------------------------------------------------------

Comment By: Aslak Hellesøy (aslak_hellesoy)
Date: 2007-07-09 18:18

Message:
In your example (both here and in your blog entry) - what does &spec_block(1) refer to? Is this some sort of block that would be implicitly available when yield_with (or similar) is used?

Could you use something more straightforward like this?

class Funky
  def self.bacon(&proc)
    yield '_how' if proc.arity == 1
  end
end

describe Funky do
  it "should yield when arity is 1" do
    lambda do 
      Funky.bacon do |s|
        s.should == '_how'
        throw :done
      end
    end.should throw_symbol(:done) # proves the bacon yielded
  end

  it "should not yield when arity is 2" do
    lambda do 
      Funky.bacon do
        throw :done
      end 
    end.should_not throw_symbol(:done)
  end
end


----------------------------------------------------------------------

Comment By: S. Potter (mbbx6spp)
Date: 2007-07-01 21:27

Message:
Great thanks, I'll attached to this ticket.

----------------------------------------------------------------------

Comment By: David Chelimsky (dchelimsky)
Date: 2007-07-01 21:25

Message:
Cool - I'll keep my eyes peeled for your patch. Go ahead and just attach it to this ticket - no need to make a new patch ticket - OK?

----------------------------------------------------------------------

Comment By: S. Potter (mbbx6spp)
Date: 2007-07-01 21:21

Message:
Actually that sounds great.  I wasn't convinced of my own
suggestions to be honest, just a starting point - wonderful!

I can try coming up with a patch after work days this week,
to contribute back.

----------------------------------------------------------------------

Comment By: David Chelimsky (dchelimsky)
Date: 2007-07-01 20:56

Message:
We don't want to add any more "should_xyz" methods because they have to get added directly to object. So it's got to be a matcher - the trick is coming up w/ good verbiage. How about this?

should yield_with(arg1,arg2)
should yield_with(no_args)

You would always have to supply at least one arg - but that arg could be no_args which would be the same method we use for mocks:

should_receive(:message).with(no_args)

WDYT?

----------------------------------------------------------------------

You can respond by visiting: 
http://rubyforge.org/tracker/?func=detail&atid=3152&aid=11949&group_id=797


More information about the rspec-devel mailing list