4

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! :-(

Matt
  • 3,682
  • 1
  • 21
  • 27
  • On which object is 'should' being called upon? Shouldn't your test have a context and description? And where is the'it' clause? This is RSpec, isn't it? – Akash May 05 '13 at 12:29
  • No, Test::Unit with shoulda - thought it best to learn "the Rails way" before getting fancy with RSpec. – Matt May 05 '13 at 12:52

1 Answers1

1

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....

Tyler Collier
  • 11,489
  • 9
  • 73
  • 80
  • Thanks for confirming. It's a shame my first test found this bug in shoulda, as it put me off this combo, and I moved on to RSpec instead (the luxury of being a test newbie an not comitted to one suite). I was amused (and a little surprised) though to read in the linked thread that shoulda has poor test coverage in places! ;-) – Matt Jun 13 '13 at 00:01
  • RSpec is a testing suite, and "shoulda" is one of its many "languages", if you will. I hear you that you might have been put off but I've used them long enough to still heartily recommend 'em both. – Tyler Collier Jun 13 '13 at 08:33