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

noreply at rubyforge.org noreply at rubyforge.org
Sat Jul 14 23:10:19 EDT 2007


Feature Requests item #12132, was opened at 2007-07-09 22: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: David Chelimsky (dchelimsky)
Date: 2007-07-15 03:10

Message:
Michael - the problem w/ using should_not raise_error is that the errors themselves would not propagate up - only should_not raise_error would fail - that would be confusing. However, Brian - consider this:

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

Why do we need to wrap the method body in anything here? Wouldn't that get you what you need?
That should give you the functionality you're looking for

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

Comment By: Michael Klishin (michaelklishin)
Date: 2007-07-14 23:14

Message:
Brian,

I may not get something here but how about lambda ... should_not raise_error?

David Ch.,

I think pass or just expect(-ed) are okay.

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

Comment By: David Chelimsky (dchelimsky)
Date: 2007-07-14 21:20

Message:
I can see the usefulness, but "succeed" doesn't speak to me. In fact, it seems to me that "satisfy" is a better word for what you're describing, but we're already using that :(

Here's a few other ideas. They probably mostly suck:

support
pass
fulfill
meet
comply

Any other ideas for the right word here?



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

Comment By: Brian Takita (btakita)
Date: 2007-07-14 20: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 20: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 20: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 22: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 22: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