I am working on step 2 of the trail Testing Fundamentals:
http://exercises.upcase.com/exercises/testing-fundamentals-testing-activerecord
I submitted my first solution, which satisfied the one test case provided to us already – that a person record is invalid without a first name.
After submitting my solution, the solution I was asked to review didn’t specifically test person.valid?
, but rather the error message found from an invalid record. I can’t find the solution again, but it was something like:
expect {
Person.create!(:first_name => nil)
}.to raise_error("ActiveRecord::RecordInvalid: Validation failed: First name can't be blank")
I really liked this solution, because in a model with more than one validation, testing the error message ensures the record was invalid for the expected reason.
But is this sort of detail necessary? Would testing expect(person).to be_invalid
be enough?