why choose for error and not for not-valid

15 views
Skip to first unread message

Roelof Wobben

unread,
Dec 9, 2014, 7:05:00 AM12/9/14
to rs...@googlegroups.com
Hello,

I like to learn Rspec once again and im using the everydayRails book.

Now I see that one of the test looks like this :

expect ( build (:contact, firstname : nill)).to_have(1).errors.on(:firstname)

but I wonder why not use this :

expect ( build (:contact, firstname : nill)).not_to_be valid

Roelof

 

Myron Marston

unread,
Dec 9, 2014, 11:41:39 AM12/9/14
to rs...@googlegroups.com
I can't speak to what the author was thinking, but if the point is to test that a `firstname` is required, the second expectation is weaker because it simply validates that the record is invalid but doesn't check why it's invalid.  Maybe `build(:contact)` returns a record that's already invalid, regardless of the `firstname` value, and there's no validation on `firstname`.  The first expectation explicitly checks that the reason the record is invalid is `firstname`, which might be the point of the test.

HTH,
Myron

Christopher Irish

unread,
Dec 10, 2014, 12:30:46 PM12/10/14
to rs...@googlegroups.com
I think the first example is better personally, it's more specific about what your expectation is asserting.  Also I'm pretty sure that "have" matcher was deprecated, dropped from rspec-expectations, and moved to this gem  https://github.com/rspec/rspec-collection_matchers .  Just a heads up that the book may be a bit dated.
Reply all
Reply to author
Forward
0 new messages