Starkers
Starkers

Reputation: 10541

Add verbosity to rspec's passing expectations

I'm using rspec rails to test my application, but I'm concerned that a few specs may actually be misleading. Can I get more information regarding passing specs in my console output to make sure the specs are testing behaviour I am happy with?

Here's how the output currently looks:

    # spec.rb

require 'spec_helper'

context "With James Bonds' car" do
    before do
        @car = {make: "Aston Martin", color: "Silver", owner: "James", age: 3}
    end

    it "should not have an age of over 1 month" do
         @car[:age].should_not == 1       
    end

end

Now, that expectation would pass, despite the car being over one month old:

$ rspec spec.rb

.

Finished in 0.12 seconds
1 examples, 0 failure, 0 pending

In order to make sure I've written good specs, how can I get ouput like this:

$ rspec spec.rb

.

Finished in 0.12 seconds
1 examples, 0 failure, 0 pending


Passing examples:

1) With James Bonds' car, it should not have an age of over 1 month

Pass: @car.age.should_not == 1       

       expected not: 1 
            got    : 3

Upvotes: 2

Views: 1805

Answers (3)

muttonlamb
muttonlamb

Reputation: 6491

Your question is a bit confusing.

If the test failed, then you would get the output you were looking for.

If you want to check the actual value that is being tested, deliberately make the test fail e.g. change it to

@car.age.should == 1

Having said that, you seem to be checking the age with a method call, but you are setting car to be an Array of Hashes. In that sense, it will never be true because the method isn't there to be checked.

Upvotes: 0

jstim
jstim

Reputation: 2432

Rspec has different formatters for test output (and you can even write your own).

Try running your specs with the documentation format.

rspec -fd spec.rb

Which will have a more verbose output containing the text you put after your describe, context and it.

That doesn't necessarily assure you are testing the right thing (that's up to you when writing tests), but it does allow you to take stock of what tests are in your suite each time you run it.

Upvotes: 1

sevenseacat
sevenseacat

Reputation: 25029

Your logic in your spec is wrong - if you want to test if the age is less than 1, test that!

@car.age.should be < 1

The test suite for any decent sized app will span hundreds if not thousands of tests - you really don't want to be wading through the output of that for every single test run.

Upvotes: 0

Related Questions