开发者

How to include ActiveRecord in gem tests?

I'm trying to use Model < ActiveRecord::Base in one of my gem tests with shoulda and I'm getting the following error

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

Without this call, I'm getting this error

开发者_开发百科

./test/model.rb:1: uninitialized constant ActiveRecord (NameError)

Help! I need a barebones AR model for my tests :)


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

Disclaimer: I'm very new to testing. This is my first gem and I wanted to make sure I was doing things the "right" way :)


If you want to use it outside of a Rails application, you need to also require 'rubygems'

I the goal of your gem is to be used inside a Rails app, and if it is your first gem, I would recommend using enginex to gain some time. Granted, it's originally supposed to help making engines, but the included testing saves a lot of hassle...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜