Testing complex-form-example helpers

I am new to test-driven development and rspec, so I am writing an
application[1] to practice. One of the patterns I am trying to
implement is from Ryan B.’ complex-form-examples[2]. Ryan’s example
doesn’t have any tests, so I am trying to write tests for the code.

I am starting with a helper function that he wrote, which makes it
easy to add fields for an association into a form. The method does a
little reflection, renders a fields_for call into a string, and then
returns a link_to_function which calls a custom javascript function
that adds the fields to the form.

My question is specifically in regard to testing this method in the
helper. I have written a test which is green, but I am not sure if it
is a good test. Since I am new, I would greatly benefit to some
opinions and critiques of my work.

Some concerns that I have:

  • I am doing a lot of method expectations, and doing so makes me think
    that I am doing something wrong.
  • My test covers functionality specific to the association between
    Evaluation and Criterion, while the code is generic.

The spec that I came up with:

The helper method those tests produced:

I have a higher-level cucumber test as well, which I am relying on for
the javascript testing:

I learned a lot about RSpec doing this, but I am sure that with a few
opinions and critiques I could learn a lot more.

Thanks,
Sam

  1. GitHub - samwgoldman/gradebook: An example test-first Rails 3.1 application w/ RSpec and Cucumber
  2. GitHub - ryanb/complex-form-examples: Various ways to handle multi-model forms in Rails.

I am new to test-driven development and rspec, so I am writing an
application[1] to practice. One of the patterns I am trying to
implement is from Ryan B.’ complex-form-examples[2]. Ryan’s example
doesn’t have any tests, so I am trying to write tests for the code.
Don’t forget that the railscast episode you are referring to is quite
old and
has a much better modern solution:
ActiveRecord::NestedAttributes::ClassMethods

Though maybe you are aware of that and still want to work out this
particular
problem manually with behavior driven development?

Patrick J. Collins
http://collinatorstudios.com

Replies inline, below.

On Mon, Jul 18, 2011 at 8:20 PM, Derek P. [email protected]
wrote:

One of the patterns I am trying to
implement is from Ryan B.’ complex-form-examples[2]. Ryan’s example
doesn’t have any tests, so I am trying to write tests for the code.

He’s updated that with a later episode series. Search his site for “nested”
and it’s the first hit, I believe.

I’ve checked this, and I believe his github project to which I
referred uses the latest material from the railscast.

fields should be added to the form.I would likely skip the helper spec and
test this with a request spec or cucumber scenario using capybara &
selenium.

I think this is a very good point. Before I dipped from my cucumber
scenario directly into my helper spec, I should have written a request
spec, or alternatively made my cucumber scenario more fleshed out.
Perhaps what led me down the wrong path was knowing the code I needed
to have before I had any tests.

Since this is an educational project and there is no deadline to speak
of, I have been very strict trying to write the minimal amount of code
to make my tests pass, and thus I have very literal tests. In real
life, I don’t think that is always possiblehow would we every write
e-mail validators?but maybe I am over emphasizing it, still.
Thoughts?

Patrick,

The example I linked to does use nested attributes. See

for the model class which uses it.

Sam

On Mon, Jul 18, 2011 at 8:03 PM, Patrick J. Collins

I am new to test-driven development and rspec, so I am writing an
application[1] to practice.

Me too, so the advice I offer below should be taken in that light. Less
advice and more, “What I think I kind of figured out.”

One of the patterns I am trying to
implement is from Ryan B.’ complex-form-examples[2]. Ryan’s example
doesn’t have any tests, so I am trying to write tests for the code.

He’s updated that with a later episode series. Search his site for
“nested”
and it’s the first hit, I believe.

I am starting with a helper function that he wrote, which makes it

easy to add fields for an association into a form. The method does a
little reflection, renders a fields_for call into a string, and then
returns a link_to_function which calls a custom javascript function
that adds the fields to the form.

The realization I’m coming to is that a helper spec isn’t necessarily
the
best thing to cover this type of thing. RSpec is about specifying
software
behavior. The behavior you want to test is, if I recall correctly from
the
episode, that when the user clicks a link in the page, new association
fields should be added to the form. I would likely skip the helper spec
and
test this with a request spec or cucumber scenario using capybara &
selenium.

It’s a fine, difficult to define, line, I think. If you’re too liberal
you
could end up testing everything via request specs. If you’re too
conservative you end up writing fairly complex specs that don’t provide
the
value they really should. As of now, I spec everything in my models and
controllers, and tend to spec only very complex helper behavior. Though
usually, I end up refactoring the model and/or controller to make the
helper
less complex instead.

As I said - this is just another n00b’s interpretation. Interested to
hear
other responses…

Some concerns that I have: