[rspec-devel] [ rspec-Feature Requests-12132 ] succeed matcher

noreply at rubyforge.org noreply at rubyforge.org
Sat Jul 14 16:28:05 EDT 2007


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

Category: expectation module
Group: None
Status: Open
Priority: 3
Submitted By: Brian Takita (btakita)
Assigned to: Nobody (None)
Summary: succeed matcher

Initial Comment:
Sometime I want to have a method that has a compound matcher.

The succeed matcher executes the block and returns true if there is no exception.

e.g.

"Foobar".should succeed do |obj|
  obj.to_s.should == "Foobar"
end # success

"Foobar".should succeed do |obj|
  obj.to_s.should == "Baz"
end # Failure

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

>Comment By: Brian Takita (btakita)
Date: 2007-07-14 13:28

Message:
Also with satisfy, if there is an ExpectationNotMetError,
the stack trace does not go into the block. The stack trace
ends where .should is called.

As a user, I would want to see which line triggers the
ExpectationNotMetError occurs.

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

Comment By: Brian Takita (btakita)
Date: 2007-07-14 13:18

Message:
Aslak, it equivalent to Test::Unit users making assertion
methods. It allows a "test dsl" to be set up.

Here is a less arbitrary example. Having matcher methods in
the Behaviour makes the intention of the Example clearer.

describe User, "#save" do
  it "works in state 1" do
    ...
    @user.should save_related_projects
  end

  it "works in state 2" do
    ...
    @user.should save_related_projects
  end

  it "works in state 3" do
    ...
    @user.should save_related_projects
  end

  def save_related_projects
    user.should succeed do |user|
      user.projects.each do |project|
        project.should be_saved_by_user
      end
    end
  end
end

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

Comment By: Brian Takita (btakita)
Date: 2007-07-14 13:11

Message:
David, satisfy depends on the return value of the block. It
has to be true. The intention of succeed is that there is no
exception in the block.

I suppose that unless the last line of the block is false or
nil, it will work, but that seems kind of side-effecty. I'm
worried that would lead to unexpected failures.

Of course, succeed is very similar to satisfy so it may not
justify adding another matcher.

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

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

Message:
What kind of method are you talking about?
Why does it matter what the succeed matcher _returns_?

Can you provide a more illustrative/complete example? I can rewrite your code as (I must be missing something):

"Foobar".to_s.should == "Foobar"
"Foobar".to_s.should == "Baz"

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

Comment By: David Chelimsky (dchelimsky)
Date: 2007-07-09 15:26

Message:
Have you tried should satisfy? It's basically what you're asking for:

http://rspec.rubyforge.org/rdoc/classes/Spec/Matchers.html#M000248

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

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


More information about the rspec-devel mailing list