【问题标题】:Unit testing with Authlogic in Rails 3.2在 Rails 3.2 中使用 Authlogic 进行单元测试
【发布时间】:2012-03-01 14:25:07
【问题描述】:

Authlogic 似乎弄乱了我的单元测试。当我尝试运行任何单元测试时,我得到:

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 文件中。

这是我的test/test_helper.rb(基于示例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

还有其他人有这个问题吗?我不知道该怎么办。

【问题讨论】:

  • 我遇到了同样的问题,但在使用 RSpec 的引擎中。

标签: ruby-on-rails-3 unit-testing authlogic


【解决方案1】:

对我来说,这是因为我有命名空间模型,例如 Qwerty::User,它链接到非命名空间表,即 users,而不是 qwerty_users,当我使用 Rails 生成器制作新模型时,例如,它创建的 Qwerty::Post 还创建了一个 Qwerty 模块,其中包含:

def self.table_name_prefix
 'qwerty_'
end

这使我的Qwerty::User 模型寻找错误的qwerty_users,因此出现“您必须建立数据库连接”错误。该表不存在。

很可能有很多方法可以得到这个错误,但我怀疑它们中的任何一种都会与 authlogic 本身直接相关。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多