[Rspec-devel] rspec, duplication in setup, etc.
David Chelimsky
dchelimsky at gmail.com
Tue Jun 13 07:39:59 EDT 2006
> From: Nate Austin <navision at gmail.com>
>
> This adds an additional level of indirection for the setup and the
> contextual naming that allows the duplication to be removed. I think
> it does so without loss of clarity, but that is, of course,
> subjective.
>
> behavior_for person do
> context of_age, citizen do
> specify "can vote in an election" do
> @person.can_vote?.should_be true
> end
> end
>
> context not_of_age, non_citizen do
> specify "can vote in an election" do
> @person.can_vote?.should_be false
> end
> end
> end
>
> person { // I don't know the proper syntax for something like this,
> so this is somewhat pseudo-ish
> // is this really necessary, or could the type be used in the
> behavior_for line?
>
> contextual_name do
> "A person"
> end
>
> construct_with(args) do
> Person.new(args) // this probably isn't needed. Maybe just get
> the type back and have framework call new? My Ruby-foo is weak.
> end
>
> }
>
> of_age {
> contextual_name do
> "18 years old"
> end
>
> constructor_contribution do
> birthdate => 18.years_ago
> end
> }
>
> citizen {
> contextual_name do
> "is a citizen"
> end
>
> constructor_contribution do
> citizen => true
> end
> }
>
>
> Also, not required for this example, but could also have an optional
> modify(object) method that would just allow you to bombard the poor
> constructed object with all sorts of nasty side-effects.
> So, framework would gather all of the various BehaviorParts for the
> context, gather the constructor_contributions, construct the object
> and put it in a variable, pass it to all of the modify() methods, and
> execute the specs
> The display would be similar to this:
>
> Person:
>
> - 18 years old, is a citizen
> - can vote in an election
> - can serve in the military
> - 17 years old, is a citizen
> - can not vote in an election
> - can not serve in the military
>
Interesting idea. Thank you for taking the time to post it.
In our conversation, one of the things you brought up was that
duplication becomes more of a problem with more complex setups
(multiple objects, etc). My instinct is that this will start to break
down in terms of clarity as you start to add more objects with varying
states.
Since the framework would have to assemble the context line in the
output, the more variables you add, the more cumbersome the output
would become:
17 years old, is a citizen, has a clean record, martial status is
single, is currently unemployed
vs
jobless single 17 year old citizen with a clean record
What if we were also interested in when a person can legally drink?
Now "of_age" needs to be rethought - perhaps "of_voting_age" and
"of_drinking_age". So then the law changes so that voting age and
military induction age differ. Now we have to break this down to
"of_age_to_vote", "of_age_to_serve" and "of_age_to_drink" and in order
to know what those are while looking at the code, I have to leave the
current context to go look at these various setups in order to know
the rules (what age IS of age to drink, etc).
Thoughts?
More information about the Rspec-devel
mailing list