在 Rails 3.2 中使用 Authlogic 进行单元测试 [英] Unit testing with Authlogic in Rails 3.2
问题描述
Authlogic 似乎搞砸了我的单元测试.当我尝试运行任何单元测试时,我得到:
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)
我的单元测试是什么并不重要.即使我的所有单元测试文件都包含 require 'test_helper'
,我仍然收到错误消息.当然,这告诉我问题可能出在我的 test/test_helper.rb
文件中.
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.
这是我的 test/test_helper.rb
(基于示例 此处):
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.
推荐答案
对我来说这是因为我有命名空间模型,例如 Qwerty::User
,它链接到非命名空间表,即users
,而不是 qwerty_users
,当我使用 Rails 生成器创建一个新模型时,例如 Qwerty::Post
它创建的还创建了一个 Qwerty
模块,其中包含:
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
这使我的 Qwerty::User
模型查找 qwerty_users
是错误的,因此出现您必须建立数据库连接"错误.该表不存在.
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.
很可能有多种方法可以导致此错误,但我怀疑其中任何一种方法都与 authlogic 本身直接相关.
Most likely there are a number of ways to get this error, but I doubt any of them will directly relate to authlogic itself.
这篇关于在 Rails 3.2 中使用 Authlogic 进行单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!