Piter Fcbk
2011-Jul-28 15:22 UTC
[rspec-users] RSpec, shoulda-matchers and Rails model attributes validations
I was trying out RSpec framework in a project and got stopped doing the unit test of a model. In particular, doing the test for the associations and the ActiveRecord validations. I started writing the validations but my tests didn''t look DRY at all. Before refactoring the tests checked out and look for other people solutions. I found out shoulda-matchers and Shoulda (which if I didn''t get it wrong is another testing framework). Actually I found another one, remarkable, but it look it doesn''t work with Rails 3. Does anyone have any advice, comments, suggestion on this matter? Right now I continue on using RSpec and shoulda-matchers. The last one mainly for testing the validations and associations of the models. Any advice and/or help will be appreciated. Thanks in advance. -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20110728/15d184c5/attachment.html>
David Chelimsky
2011-Jul-28 16:01 UTC
[rspec-users] RSpec, shoulda-matchers and Rails model attributes validations
On Jul 28, 2011, at 10:22 AM, Piter Fcbk wrote:> I was trying out RSpec framework in a project and got stopped doing the unit test of a model. In particular, doing the test for the associations and the ActiveRecord validations. > I started writing the validations but my tests didn''t look DRY at all. Before refactoring the tests checked out and look for other people solutions. > I found out shoulda-matchers and Shoulda (which if I didn''t get it wrong is another testing framework). Actually I found another one, remarkable, but it look it doesn''t work with Rails 3. > > Does anyone have any advice, comments, suggestion on this matter? > > Right now I continue on using RSpec and shoulda-matchers. The last one mainly for testing the validations and associations of the models. > Any advice and/or help will be appreciated. > > Thanks in advance.rspec + shoulda matchers is a fairly common pairing these days, so you''re not alone. Personally, I use them for validations (which are behavior), but not for associations (which are structure). Rather than specifying, for example, that a team has many players, I just use the players collection in an example that uses them: describe Team do it "does not have any open spots if there are 30 players" do team = Factory(:team) 30.times { team.players << Factory(:player) } team.should_not have_opening end end HTH, David -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20110728/117de525/attachment-0001.html>
Sam Goldman
2011-Jul-28 18:42 UTC
[rspec-users] RSpec, shoulda-matchers and Rails model attributes validations
I think that there is a lot of value to projects like shoulda-matchers (which is separate from shoulda, the testing framework) and remarkable. The idea is that you should not be testing the framework (rails). That is, you should not be testing that defining a has_many association creates a method on your model. You still need to test all of your domain logic, which David''s example shows. Also, remarkable does work in rails 3. I am using it because it has a matcher for accepts_nested_attributes_for, while shoulda-matchers does not. Using this line in my Gemfile works: gem ''remarkable_activerecord'', ''~> 4.0.0.alpha2'' Sam On Thu, Jul 28, 2011 at 11:22 AM, Piter Fcbk <piter.fcbk at gmail.com> wrote:> I was trying out RSpec framework in a project and got stopped doing the unit > test of a model. In particular, doing the test for the associations and the > ActiveRecord validations. > I started writing the validations but my tests didn''t look DRY at all. > Before refactoring the tests checked out and look for other people > solutions. > I found out shoulda-matchers and Shoulda (which if I didn''t get it wrong is > another testing framework). Actually I found another one, remarkable, but it > look it doesn''t work with Rails 3. > Does anyone have any advice, comments, suggestion on this matter? > Right now I continue on using RSpec and shoulda-matchers. The last one > mainly for testing the validations and associations of the models. > Any advice and/or help will be appreciated. > Thanks in advance. > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >