Unit testing with Authlogic in Rails 3.2

2019-06-11 03:27发布

Authlogic seems to be messing up my unit tests. When I try to run any unit test, I get:

authlogic/acts_as_authentic/base.rb:31:in `acts_as_authentic': You must establish a database connection before using acts_as_authentic (StandardError)

It doesn't matter what my unit test is. Even if all my unit test file contains is require 'test_helper', I still get the error. This, of course, tells me that the problem is probably in my test/test_helper.rb file.

Here's my test/test_helper.rb (based on the example here):

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

class ActiveSupport::TestCase
  # Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order.
  #
  # Note: You'll currently still have to declare fixtures explicitly in integration tests
  # -- they do not yet inherit this setting
  fixtures :all

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

class ActionController::TestCase
  setup :activate_authlogic
end

Is anybody else having this problem? I don't know what to do.

1条回答
贼婆χ
2楼-- · 2019-06-11 04:13

For me it was because I had namespaced models, e.g Qwerty::User, which where linked to non-namespaced tables, i.e users, not qwerty_users, and when I used the Rails generator to make a new model, e.g Qwerty::Post it created also created a Qwerty module which contained:

def self.table_name_prefix
 'qwerty_'
end

This was then making my Qwerty::User model look for qwerty_users which was wrong, hence the "You must establish a database connection" error. The table did not exist.

Most likely there are a number of ways to get this error, but I doubt any of them will directly relate to authlogic itself.

查看更多
登录 后发表回答