Topic: Problem Using `rake test`

Hey to all,

I wonder how to setup testing in my rails apps. When I run `rake test`, first thing odd, it launch a bunch of `CREATE TABLE` against my dev. database (hum.. do not like this..). So I launch `rake test RAILS_ENV=test` and I even try `bundle exec rake test RAILS_ENV=test`. Now, the `CREATE TABLE` is against my test database but all fails with this error :

    ** Execute test:units
    test/unit/category_test.rb:5:in `test': unknown command 't' (ArgumentError)
         from test/unit/category_test.rb:5:in `<class:CategoryTest>'

I have used basic generator in Rails 3 and do not change anything. So I have this in caterogy_test.rb :

    require 'test_helper'

    class CategoryTest < ActiveSupport::TestCase
       # Replace this with your real tests.
      
       test "the truth" do
           assert true
       end
    end

I use Rails 3.0.7 and basic config.

Any ideas ?

Re: Problem Using `rake test`

Should be able to run migrations in the test environment.

rake db:migrate RAILS_ENV=test

You shouldn't have to specify running the tests in that environment.

In terms of the error message, that is really strange. I suggest to try running the migrations, and run the tests again. Try looking at the guides as well http://guides.rubyonrails.org/testing.html. It suggest running rake db:test:load, which recreates the database from the current db/schema.rb

Re: Problem Using `rake test`

Also try rake test:db:prepare, this should run the migrations against the test database.

The unknown command t (argument error) will most likely be a problem with one of your migration files

What you want and what you need are too often not the same thing!
When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.
(Quote by me 15th July 2009)

Re: Problem Using `rake test`

I am becoming crazy, made a lot of tries, neither seems to work. When I start a new application with a few things, `rake test` works fine but when I try this on my current one, it launch always against my dev. db and do not work at all. I have tried to edit the test files, to revert them back, try to remove/setup test db with different ways, try different rake version, compare a lot of things on one side my current application and on the other a brand new one... Found nothing.. Help !

Re: Problem Using `rake test`

Sounds lame, but is it normal that `rake` does the same thing than `rake test` ?

Re: Problem Using `rake test`

either Rails is not recognising the test environment or your database.yml is not correctly defined.
do you have something strange in your environment.rb to force a development environment?

What you want and what you need are too often not the same thing!
When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.
(Quote by me 15th July 2009)

Re: Problem Using `rake test`

Yeah, I had watched everything maybe a dozen of times..

Sounds odds, while I continue to work on what's wrong, I realize that every-time I run `rake test`, it does stuff on the dev environment and not the test one (watching the logs). It does this on my computer OSX and on our server FreeBSD for all the Rails 3.0.7 apps. Are you sure `rake test` is supposed to work on the test environment by default ?

Last edited by Hartator (2011-06-01 17:05:21)

Re: Problem Using `rake test`

Well, after doing some considerable experimenting with 2.3.8, 3.0.7 and 3.0.8 RC1  I am seeing odd behaviour.
I believe I already asked this but can you post your complete database.yml file (minus any passwords) I think I may have a solution but I'm not sure

What you want and what you need are too often not the same thing!
When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.
(Quote by me 15th July 2009)

Re: Problem Using `rake test`

I do not have changed yet the db config :

# SQLite version 3.x
#   gem install sqlite3
development:
  adapter: sqlite3
  database: db/development.sqlite3
  pool: 5
  timeout: 5000

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  adapter: sqlite3
  database: db/test.sqlite3
  pool: 5
  timeout: 5000

production:
  adapter: sqlite3
  database: db/production.sqlite3
  pool: 5
  timeout: 5000

Re: Problem Using `rake test`

HHmmm! That looks fine.
Let's recap
Can you confirm the following
1) You are running rails 3.0.7
2) You have not set anything to force development environment (Rails.env is not set anywhere in particular)
3) You run rake test and your development.sqlite3 database gets wiped
4) Your development.log file gets the test entries and your test.log file is empty
5) This is happening on more than one rails 3.0.7 app

It certainly seems like you are running rake test in the development environment. I'm stumped

I'm having no issues with this at all, I found an anomoly with commented out database.yml configuration settings but this is not your issue.

What you want and what you need are too often not the same thing!
When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.
(Quote by me 15th July 2009)

Re: Problem Using `rake test`

1) You are running rails 3.0.7 - yep
2) You have not set anything to force development environment (Rails.env is not set anywhere in particular) - yep (check everything that I can have done, even dump my lib/tasks to zero)
3) You run rake test and your development.sqlite3 database gets wiped - strange thing it doesn't empty it, just run some CREATE TABLE, and write INSERT INTO some migrations stuff.
4) Your development.log file gets the test entries and your test.log file is empty - yep
5) This is happening on more than one rails 3.0.7 app - yep, I have also try and our server running freeBSD and clean install, my dev. computer is OSX with rvm intalled.

Yeah, do not understand why it's doing that.

Re: Problem Using `rake test`

I'm pretty sure that the test database is the only db that will get cleared. I think this is based on the specific word "test" in your database.yml file so that question might have been a red herring

What test framework are you using?
Do your tests behave as expected in all other respects?
I think you may have found a bug and you should report this behaviour to Rails dev team https://github.com/rails/rails/issues

What you want and what you need are too often not the same thing!
When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.
(Quote by me 15th July 2009)

Re: Problem Using `rake test`

I have Test::Unit ( or minitest since I'm using ruby 1.9.2)
I have a bug in my current application but I think it's not related with the `rake` think because it does it also on brand new one.
You think I'll open a ticket ?

Re: Problem Using `rake test`

Yes

What you want and what you need are too often not the same thing!
When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.
(Quote by me 15th July 2009)

Re: Problem Using `rake test`

Ok, I will do that.

Re: Problem Using `rake test`

Is that ok ?
https://github.com/rails/rails/issues/1491

Re: Problem Using `rake test`

That's fine, I guess you may well get some questions and will need to follow up on that at some point.
I guess you could no delve around in the rails source code and see where the problem lies, fix it and ask the Rails team to implement the fix.

Good luck

What you want and what you need are too often not the same thing!
When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.
(Quote by me 15th July 2009)

Re: Problem Using `rake test`

Just a quick thought
Do you have

ENV["RAILS_ENV"] = "test"

defined in your test_helper.rb file?

What you want and what you need are too often not the same thing!
When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.
(Quote by me 15th July 2009)