0

I want to load the test environment for Rails to run my minitest tests.

I require config/environment.rb after setting RAILS_ENV to 'test'.

It seems that this is not the correct way to do it because the test database doesn't seem to be created.

I know this is a pretty noobish question, but I don't see anyone else really asking it.

*Code in my test/test_helper.rb

require 'minitest/autorun'

ENV['RAILS_ENV'] = 'test'

require_relative '../config/environment'

*Basic Boiler Plate and Sanity Test in test/models/users_test.rb

require_relative '../test_helper.rb'

class UserModelTest < MiniTest::Test

  def setup
    @user = User.new
    @user.email = 'me@example.com'
    @user.password = 'password'
    @user.save
  end

  def test_sanity
    assert true
  end

end

*The error I get from the setup method above that makes me think the test db is not being created and migrated

Error:
UserModelTest#test_sanity:
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR:  relation "users" does not exist
LINE 5:                WHERE a.attrelid = '"users"'::regclass
                                      ^
:               SELECT a.attname, format_type(a.atttypid, a.atttypmod),
                     pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod
                FROM pg_attribute a LEFT JOIN pg_attrdef d
                  ON a.attrelid = d.adrelid AND a.attnum = d.adnum
                WHERE a.attrelid = '"users"'::regclass
                  AND a.attnum > 0 AND NOT a.attisdropped
                ORDER BY a.attnum

    /home/john/.rvm/gems/ruby-2.1.1@mangam/gems/activerecord-4.1.0/lib/active_record/connection_adapters/postgresql_adapter.rb:815:in `async_exec'
.
.
.
/home/john/.rvm/gems/ruby-2.1.1@mangam/gems/activerecord-4.1.0/lib/active_record/inheritance.rb:23:in `new'
   test/models/users_test.rb:6:in `setup'

1 Answers1

0

Sometimes the test database can get out of sync. I'm not really sure what causes it... but I can usually fix the problem by running the database-related rake commands under the test environment. In your case, it looks like the migrations are just out of sync, so try:

RAILS_ENV=test bundle exec rake db:migrate

Furthermore, in Rails 4.1, here's what the default test_helper.rb file should look like, so you may want to adjust yours:

ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'

class ActiveSupport::TestCase
  # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
  fixtures :all

  # Add more helper methods to be used by all tests here...
end

Then in your test file, you can just use require "test_helper" instead of require_relative '../test_helper.rb'.

pdobb
  • 17,688
  • 5
  • 59
  • 74
  • Thanks for the answer. I think I will drop, create and migrate the test db in the test_helper. I'm not sure about using ActiveSupport for tests, though. Are there real benefits for doing so? I think I will keep things simple, so just using minitest assertions should be okay. Thanks again –  Aug 25 '14 at 12:38