Hi. I too have had a similar need to tests validations that are
dependent on object state. Unless there's already a good idiomatic
way of handling this I was thinking that maybe adding an
optional :attributes or :object option to the Active Record shoulda
helpers. Here are some examples:
A) should_require_acceptance_of :terms_and_conditions, :attributes =>
{ :signup_step => 2 }
B) should_require_attributes :password_confirmation, :object =>
Person.new(:password => "something")
I can see A becoming ugly fast and B isn't much better. Perhaps
something like this:
C) should_require_attributes :password_confirmation, :object =>
"@person"
...where @person is constructed in the setup.
D) Maybe a combination of B and C where the logic is based on whether
a string (to be eval'd) is passed.
None of these really work well when you have a set of validations for
a particular object state.
E) Another alternative I can think of is to standardize on a specially
named instance variable (e.g. @shoulda_object) that is specified in
the setup. Use it to test the validation instead of '
klass.new' if it
exists.
After talking through this, D or E seem reasonable. D doesn't feel
optimal. E is simple, but I'm unsure about the special variable.
I'd be willing to work on this if any of it seems worthwhile to
others.
What does everyone else think?
-Ryan