I am coming from a Spring/hibernate background. I have noticed that Rails has no dao and service layers. This really speeds up development, but I don’t know where to put my tests sometimes.
Right now, I’ve been putting my model methods and validation tests in the main model spec. This file is already fairly large.
Where is the ‘standard’ place to test queries? I can imagine myself making a lot of fixtures/dummy data to make sure my queries are working as expected (probably an even better idea since I am new to rails). These are not really needed for the basic model logic and validation tests.
If you could offer some advice as to where put these tests, the best approach to testing queries using rails (especially ones with multiple joins!), and maybe some basic guidelines of how it might different from doing it with DBunit/spring/hibernate, that would be great.
Thanks!
I used to work with hibernate too. The ActiveRecord way is very different from hibernate. You need to set your mind free, for better or for worse. In java and Hibernate you often have the aggregate root and the object graph. Generally, both the object graphs and code base are smaller in ruby somehow too. I don’t know your particular case, so I’ll tread carefully, but I warn you to try fit ruby, and rails, to your java habits.
You may use custom directories with rspec to organize in a way that makes sense for you and your team.
and you may have subdirectories, automatically getting picked up by rspec, like
spec/models/account/..The spec will automatically be picked up by
rake specorrspec spec. I just wrote a simple example above, as I don’t know your case. Do you define scopes with queries, or define specialized methods?I strongly recommend abandoning the fixtures (same as the inserts – anti pattern, to me) for something more refactorable, like factories. I like factory_girl. It let’s your app evolve in a more agile manner, IMO.
EDIT:
adding my spec_helper.rb with settings for enable/disable automatic cleanup
I add the variable
skip_database_cleanso that I can enable/disable the autocleanup after each spec (each “it”).