【问题标题】:How to include ActiveRecord in gem tests?如何在 gem 测试中包含 ActiveRecord?
【发布时间】:2011-05-18 20:27:52
【问题描述】:

我正在尝试在我的一项 gem 测试中使用 Model < ActiveRecord::Base,但出现以下错误

./test/model.rb:1:in `require': no such file to load -- active_record (LoadError)

如果没有这个调用,我会收到这个错误

./test/model.rb:1: 未初始化的常量 ActiveRecord (NameError)

帮助!我需要一个准系统 AR 模型来进行测试 :)


my_gem/test/model.rb

require 'active_record' # <= this fails

class Model < ActiveRecord::Base # <= I need a barebones AR model here
  acts_as_flaggable
end

my_gem/test/test_acts_as_flaggable.rb

require 'helper'
require 'model'

class TestActsAsFlaggable < Test::Unit::TestCase
  context "a model" do

    setup do
      @model = Model.new
    end

    should "be able to set flag" do
      @model.flag[:foo] = "bar"
      assert_equal "bar", @model.flag[:foo]
    end

    should "get default value for unset flags" do
      @model = User.new
      assert_equal false, @model.flag[:some_unset_flag]
    end

  end
end

免责声明:我对测试很陌生。这是我的第一颗宝石,我想确保我以“正确”的方式做事:)

【问题讨论】:

  • 我相信你已经安装了 activerecord gem,你可以从 irb 要求它?

标签: ruby-on-rails-3 unit-testing activerecord gem shoulda


【解决方案1】:

如果你想在 Rails 应用程序之外使用它,你还需要require 'rubygems'

我的 gem 的目标是在 Rails 应用程序中使用,如果它是你的第一个 gem,我建议使用 enginex 来获得一些时间。诚然,它原本是用来帮助制造引擎的,但附带的测试省去了很多麻烦……

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-31
    • 1970-01-01
    • 1970-01-01
    • 2016-09-21
    相关资源
    最近更新 更多