> 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@rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
_______________________________________________ rspec-users mailing list rspec-users@rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users