What we are doing here is making an object that quacks like an
ActiveRecord::Base object but has no database table backing it. As examples: a
search object, a credit card object, or a remote user object. To do this, you
need id, new_record?, and the attributes must work in a specific fashion.
You can also have errors produce an ActiveRecord::Errors instance so user
errors will show in forms and whatnot.

However you may not have noticed that Rails 2.3.4 broke API compatibility for ActiveRecord::Errors. If
your ActiveRecord::Base-like class provides an #errors instance method, it
must now provide these class methods:

self_and_descendants_from_active_record, which produces an array of classes.

human_name, which takes an optional hash and produces a string.

human_attribute_name, which takes a string and optional hash and produces a
string.

The last two of these methods are useful in your day-to-day Rails knowledge (the
first is an internal, undocumented method used by Rails to produce the class
itself and its parents, up to and excluding ActiveRecord::Base).

human_name is used to produce a humanized string representing the class/table
name. If your class is named AuthenticationRecord, .human_name will produce
"Authenticationrecord". Any options you pass to this are normally sent to
I18n.translate.

human_attribute_name is used to map an attribute to a human-readable string.
For example, the attribute created_at is mapped to "Created at". Any options
passed to this are normally sent to I18n.translate.

Here are some tests that need to pass now:

classSearchTest<ActiveSupport::TestCaseshould"conform to the ActiveRecord::Errors interface"doassert_respond_toSearch,:self_and_descendants_from_active_recordassert_respond_toSearch,:human_nameassert_respond_toSearch,:human_attribute_nameassert_equal[Search],Search.self_and_descendants_from_active_recordassert_equal"Search",Search.human_nameassert_equal"Published at",Search.human_attribute_name('published_at')endend

If you enjoyed this post, you might also like:

Want to level up your testing game?
Learn about testing Rails applications and TDD
in our new book
Testing Rails.
The book covers each type of test in depth,
intermediate testing concepts,
and anti-patterns that trip up even intermediate developers.