• 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

While creating AR extending plugin, I had some trouble to test it. Because testing a such plugin needs creating db and its absurd. So here's my solution. 

Tip #1: First, extend the AR, and name it as a MockBase. Then extend MockBase to test your class. This prevents other tests like your rails app or other plugins from failing.

Tip #2. Requiring environment.rb loads all rails env this includes AR. Thus you can you use AR in your test class.

rails>>
#acts_as_notifiable_test.rb
begin
require File.dirname(FILE) + '/../../../../config/environment'
rescue LoadError
require 'rubygems'
gem 'activerecord'
require 'active_record'
end

class MockBase < ActiveRecord::Base; end
MockBase.class_eval do
alias_method :save, :valid?
def self.columns() @columns ||= []; end

def self.column(name, sql_type = nil, default = nil, null = true)
columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type, null)
end
end

class MockModel < MockBase
acts_as_notifiable :callback => :after_create, :message => "hello", :recipients => "test@gmail.com"
end

class ActsAsNotifiableTest < Test::Unit::TestCase
def test_option_message_string
assert_equal "hello", MockModel.new.instance_eval{jabber_message}
end
<<--

###Reference

posted by hibi hibi on Fri 21 Mar 2008 at 00:00 with 0 comments