Problem running RSpec tests with Rails

Hi

Not sure if this is a RSpec problem or Rails but I believe is more a
RSpec situation.

What happens is that when I run my RSpecs tests all the BD is recreated,
including the “schema_migration” table, because of this I get a problem
saying:

You have 29 pending migrations:
20100628100855 CreateCustomers
20100628103228 CreateAccounts
20100628172155 CreateCfgEntities

etc

It seems rails notices that the schema_migration tables is empty and
that are migration scripts to run. This shouldn’t be needed because
Rails uses schema.rb to recreate the table, right?

I’m using JRuby with JDBC connection by the way.

Anyone know how to resolve this?

On Jul 26, 2010, at 5:12 pm, Bruno C. wrote:

Not sure if this is a RSpec problem or Rails but I believe is more a
RSpec situation.

What happens is that when I run my RSpecs tests all the BD is recreated,

Anyone know how to resolve this?

Hi Bruno

Are you running rake spec as opposed to the spec command? The default
Rails spec task depends on the “db:test:prepare” task, which bombs your
DB. I fixed this in my project with a file
“lib/tasks/rspec_fixes.rake”:

class Rake::Task
def overwrite(&block)
@actions.clear
enhance(&block)
end
end

Rake::Task[‘db:test:prepare’].overwrite do
# We don’t want to run migrations or load the schema!!!
end

HTH

Ash


http://www.patchspace.co.uk/
http://www.linkedin.com/in/ashleymoran

On Jul 26, 2010, at 11:12 AM, Bruno C. wrote:

20100628100855 CreateCustomers
20100628103228 CreateAccounts
20100628172155 CreateCfgEntities

etc

It seems rails notices that the schema_migration tables is empty and
that are migration scripts to run. This shouldn’t be needed because
Rails uses schema.rb to recreate the table, right?

The test database, yes, but it won’t get that far if you have any
pending migrations in the development database.

If you run “rake spec --trace” you’ll see that it runs db:test:prepare,
which runs db:abort_if_pending_migrations.

HTH,
David

Thanks for the answers.

What both solutions (from Ashley and David) do is not modify the BD in
anyway, so nothing gets dropped and nothing is created. This resolves
the problem but what if I want a clean installation in each test run? Is
there a way to keep my “schema_migrations” table or remove the check for
migration scripts? I would still like to recreate the rest of the
tables.

On Jul 26, 2010, at 11:50 AM, Ashley M. wrote:

Anyone know how to resolve this?

Hi Bruno

Are you running rake spec as opposed to the spec command? The default Rails spec task depends on the “db:test:prepare” task, which bombs your DB.

The test DB, not the development DB, right? If it’s bombing your dev DB
then please file a bug report and we can sort it out.

I fixed this in my project with a file “lib/tasks/rspec_fixes.rake”:

class Rake::Task
def overwrite(&block)
@actions.clear
enhance(&block)
end
end

Rake::Task[‘db:test:prepare’].overwrite do

Another approach would be:

Rake::Task[‘spec’].prerequisites.clear

Cheers,
David

Bruno C. wrote:

Thanks for the answers.

What both solutions (from Ashley and David) do is not modify the BD in
anyway, so nothing gets dropped and nothing is created. This resolves
the problem but what if I want a clean installation in each test run? Is
there a way to keep my “schema_migrations” table or remove the check for
migration scripts? I would still like to recreate the rest of the
tables.

By the way, I’m using: “rake spec --trace” and the result was:

** Invoke spec (first_time)
** Invoke db:test:prepare (first_time)
** Invoke db:abort_if_pending_migrations (first_time)
** Invoke environment
** Execute db:abort_if_pending_migrations
You have 12 pending migrations:
20100628100855 CreateCustomers

Run “rake db:migrate” to update your database then try again.

On Jul 26, 2010, at 12:36 PM, Bruno C. wrote:

Thanks for the answers.

What both solutions (from Ashley and David) do is not modify the BD in
anyway, so nothing gets dropped and nothing is created. This resolves
the problem but what if I want a clean installation in each test run?

So is your goal here to have the rake task(s) ignore the fact that you
have pending migrations, but still have it reset the test database so
it’s schema looks like the development database schema?

On 26 Jul 2010, at 6:36 PM, Bruno C. wrote:

What both solutions (from Ashley and David) do is not modify the BD in
anyway, so nothing gets dropped and nothing is created. This resolves
the problem but what if I want a clean installation in each test run? Is
there a way to keep my “schema_migrations” table or remove the check for
migration scripts? I would still like to recreate the rest of the
tables.

One way is to avoid rake entirely - you could run the specs with spec spec or even just use autotest[1].

If you want to just empty the data from the tables you need
database_cleaner[2].

If you want the database rebuilt as necessary when migrations change,
you could use my database_resetter[3], plug plug :smiley:

Let me know if you’re trying to do something other than one of these.
(It’s late, I have a habit of talking nonsense when I’m tired…)

HTH

Ash

[1] eg http://ph7spot.com/musings/getting-started-with-autotest
[2] http://github.com/bmabey/database_cleaner
[3] http://rubygems.org/gems/database_resetter


http://www.patchspace.co.uk/
http://www.linkedin.com/in/ashleymoran

On Jul 27, 2010, at 4:10 AM, Bruno C. wrote:

have pending migrations, but still have it reset the test database so
it’s schema looks like the development database schema?

Exactly.

OK. This is not Rails’ intent (which is why
‘db:abort_if_pending_migrations’ is a prereq of ‘db:test:prepare’), but
you can use the same technique I described earlier on ‘db:test:prepare’
instead of ‘spec’:

Rake::Task[‘db:test:prepare’].clear_prerequisites

Now you should get what you’re looking for.

Cheers,
David

David C. wrote:

On Jul 26, 2010, at 12:36 PM, Bruno C. wrote:

Thanks for the answers.

What both solutions (from Ashley and David) do is not modify the BD in
anyway, so nothing gets dropped and nothing is created. This resolves
the problem but what if I want a clean installation in each test run?

So is your goal here to have the rake task(s) ignore the fact that you
have pending migrations, but still have it reset the test database so
it’s schema looks like the development database schema?

Exactly.

I’m ok with the previous solution, it’s something we can do but I would
like to know what I could do if I wanted to go by my first plan.

David C. wrote:

On Jul 27, 2010, at 4:10 AM, Bruno C. wrote:

have pending migrations, but still have it reset the test database so
it’s schema looks like the development database schema?

Exactly.

OK. This is not Rails’ intent (which is why
‘db:abort_if_pending_migrations’ is a prereq of ‘db:test:prepare’), but
you can use the same technique I described earlier on ‘db:test:prepare’
instead of ‘spec’:

Rake::Task[‘db:test:prepare’].clear_prerequisites

Now you should get what you’re looking for.

Cheers,
David

Yeah, it worked exactly like you said. Now I can choose between the two
approaches.

Thanks.

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