Running Ruby/Rails Specs with database inserts from non-default spec sub-directory

Hi everyone!

I have a question on how to setup my tests a bit differently than the
defaults.

By default, Rails with RSpec installed provides you with directories
such as spec/models, spec/controllers, etc.

When i use before(:each), I notice that Rails properly cleans out the
database in these specs automatically.

However, I created a new sub-directory called spec/queries. My
intention was to put all of my query-related tests here so that the
database setup code didn’t run with the model validation and business
logic tests (for speed and organization reasons). I will have a lot of
complex queries that will need to be tested against a mini-database…
so this seems like the right thing to do (or is there a better
solution?)

When I put these tests in spec/queries folder and run the tests
multiple times, Rails complains that data is not unique and so on…
so I am guessing that the data is not getting cleaned out now? Yes, I
am using factory girl with sequences, so this is probably a database
cleaning problem. I didn’t have it before when these same tests were
in ‘specs/models’.

  1. Do I need to manually clean out the data since I moved the specs
    into it’s own spec/squeries subdirectory?

  2. Is there any way to include spec/queries to the list of directories
    that get auto-cleaned, much like spec/models does?

Thanks

On Wed, May 11, 2011 at 1:54 PM, egervari [email protected]
wrote:

so I am guessing that the data is not getting cleaned out now? Yes, I
am using factory girl with sequences, so this is probably a database
cleaning problem. I didn’t have it before when these same tests were
in ‘specs/models’.

  1. Do I need to manually clean out the data since I moved the specs
    into it’s own spec/squeries subdirectory?

  2. Is there any way to include spec/queries to the list of directories
    that get auto-cleaned, much like spec/models does?

There is a gem called database_cleaner which might help in your case to
run
before your test/suite.

There is a gem called database_cleaner which might help in your case to run
before your test/suite.

Well, I’m looking into that now… and it mostly works (But not
quite… it seems like there are some records hanging around still).

Why do I not need the database cleaner for the model tests… but I do
for this new spec/queries directory? Can I simply tell rails to treat
this directory the same as it treats spec/models? The tests work in
that folder.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs