[rspec-users] Failure Messages in RSpec

David Chelimsky dchelimsky at gmail.com
Tue Sep 4 13:50:36 EDT 2007

On 9/4/07, Geoffrey Wiseman <geoffrey.wiseman at gmail.com> wrote:
> Having used JUnit and Test::Unit, I'm quite used to having the ability to
> insert a failure message, which helps when tests fail.
> For instance, the example RSpec that is generated for a model class
> specifies that the model class is valid.  Assuming this were supposed to be
> true, and it failed, I've now got to duplicate the code in the test in order
> to find out why it wasn't valid.
> Whereas if I were writing the same code in Test::Unit, I might write:
>   assert model.valid?, "Expected model to be valid, but found these errors:
> #{model.errors}"
> This means that when the model validation fails, I know /why/.  I don't see
> an easy way to include these sorts of messages in RSpec, which seems likely
> to cause me to waste time on test failures.  Am I missing something?  How
> are experienced RSpec users resolving this?

I come from the same background as you, so I hear where you're coming
from. We made a conscious decision, however, not to support custom
messages almost two years ago and I'm not sure if its ever even come
up before. If it has, it was a long time ago.

If you follow the conventions of one expectation per example, and your
example is well named, this is less of a problem. Here's a common

describe Person do
  def valid_attributes
    {:name => 'joe smith'}
  before(:each) do
    @person = Person.new(valid_attributes)
  it "should be valid with valid attributes" do
    @person.should be_valid
  it "should be invalid with no name" do
    @person.name = nil
    @person.should_not be_valid

Together, these different examples help to tell the whole story, and
when one example fails you know why it's failing - its just that the
message is in the example's name instead of a custom assertion

Make sense?

> Thanks,
>   - Geoffrey
> --
> Geoffrey Wiseman
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users

More information about the rspec-users mailing list