Why is this test result reversed?

I am writing my first tests using Test: Unit and Shoulda, so this may be a simple misunderstanding on my part, but given the Pages model, which does not contain validation, and the 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 (in the absence of 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 Did not expect errors to include "can't be blank" when slug is set to nil be reversed. You should not read: Expected errors to include "can't be blank" because if the check I'm testing for existence, you expect an error from checking that slug "cannot be empty" when slug is nil.

The test passes if I add confirmation, but first I want it to work correctly!

Also, why does the first test pass? My Pages model does not contain belongs_to association!

Testing sucks !: - (

+4
source share
1 answer

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

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

+1
source

Source: https://habr.com/ru/post/1479232/


All Articles