Question

I'm writing my first tests using Test:Unit and Shoulda, so this may be a simple misunderstanding on my part, but given a Pages model that contains no validation, and a test:

#test/unit/page_test.rb    
require 'test_helper'

class PageTest < ActiveSupport::TestCase
  should belong_to(:site)
  should validate_presence_of(:slug)
end

The second test fails as expected (given there's no validation in the Model):

# Running tests:

[2/2] PageTest#test: Page should require slug to be set.  = 0.02 s
  1) Failure:
test: Page should require slug to be set. (PageTest) [/Users/Matt/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/shoulda-context-1.1.1/lib/shoulda/context/context.rb:339]:
Did not expect errors to include "can't be blank" when slug is set to nil, got error: 

Finished tests in 0.115436s, 17.3256 tests/s, 17.3256 assertions/s.
2 tests, 2 assertions, 1 failures, 0 errors, 0 skips

However the error message Did not expect errors to include "can't be blank" when slug is set to nil seems backwards. Shouldn't that read: Expected errors to include "can't be blank", because if the validation I'm testing for existed, you'd expect an error from the validation that slug "can't be blank" when slug is nil.

The test passes if I add the validation, but I'd like to see it fail correctly first!

Also, why does the first test pass? My Pages model doesn't contain a belongs_to association!

Testing sucks! :-(

Was it helpful?

Solution

This was a bug in older versions of shoulda. You can see this thread for more info.

When I updated to the latest version (2.2.0 right now), I see the correct behavior and message like Expected errors to include....

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top