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 Bates'' 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: https://github.com/samwgoldman/gradebook/blob/59b48ed4c973d135f54086ad483c09b6e34aec75/spec/helpers/application_helper_spec.rb The helper method those tests produced: https://github.com/samwgoldman/gradebook/blob/59b48ed4c973d135f54086ad483c09b6e34aec75/app/helpers/application_helper.rb I have a higher-level cucumber test as well, which I am relying on for the javascript testing: https://github.com/samwgoldman/gradebook/blob/59b48ed4c973d135f54086ad483c09b6e34aec75/features/create_evaluation.feature#L26 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. https://github.com/samwgoldman/gradebook 2. https://github.com/ryanb/complex-form-examples
Patrick J. Collins
2011-Jul-19 00:03 UTC
[rspec-users] 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 Bates'' 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: http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html 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
> 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 Bates'' 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:> * 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: > > https://github.com/samwgoldman/gradebook/blob/59b48ed4c973d135f54086ad483c09b6e34aec75/spec/helpers/application_helper_spec.rb > > The helper method those tests produced: > > https://github.com/samwgoldman/gradebook/blob/59b48ed4c973d135f54086ad483c09b6e34aec75/app/helpers/application_helper.rb > > I have a higher-level cucumber test as well, which I am relying on for > the javascript testing: > > https://github.com/samwgoldman/gradebook/blob/59b48ed4c973d135f54086ad483c09b6e34aec75/features/create_evaluation.feature#L26 > > 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. https://github.com/samwgoldman/gradebook > 2. https://github.com/ryanb/complex-form-examples > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20110718/237cdb84/attachment.html>
Patrick, The example I linked to does use nested attributes. See https://github.com/ryanb/complex-form-examples/blob/master/app/models/survey.rb for the model class which uses it. Sam On Mon, Jul 18, 2011 at 8:03 PM, Patrick J. Collins <patrick at collinatorstudios.com> wrote:>> 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 Bates'' 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: > http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html > > 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 > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
Replies inline, below. On Mon, Jul 18, 2011 at 8:20 PM, Derek Prior <derekprior at gmail.com> wrote:>> One of the patterns I am trying to >> implement is from Ryan Bates'' 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.>> 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.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 possible?how would we every write e-mail validators??but maybe I am over emphasizing it, still. Thoughts?