Unit testing with Authlogic in Rails 3.2

假装没事ソ 提交于 2019-12-08 09:35:36

问题


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:


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.



来源:https://stackoverflow.com/questions/9517887/unit-testing-with-authlogic-in-rails-3-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!