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 environmen
1 #acts_as_notifiable_t est.rb 2 begin 3 require File.dirname(__FILE__) + '/../../../ ../config/ environmen t' 4 rescue LoadError 5 require 'rubygems' 6 gem 'activereco rd' 7 require 'active_rec ord' 8 end 9 10 class MockBase < ActiveReco rd::Base; end 11 MockBase.class_eval do 12 alias_meth od :save, :valid? 13 def self.colum ns() @columns ||= []; end 14 15 def self.colum n(name, sql_type = nil, default = nil, null = true) 16 columns << ActiveReco rd::Connection Adapters::Column.new(name.to_s, default, sql_type, null) 17 end 18 end 19 20 class MockModel < MockBase 21 acts_as_no tifiable :callback => :after_crea te, :message => "hello", :recipients => "test@gmail.com" 22 end 23 24 class ActsAsNoti fiableTest < Test::Unit ::TestCase 25 def test_optio n_message_ string 26 assert_equ al "hello", MockModel.new.instance_e val{jabber_mes sage} 27 end
Reference
posted by
hibi
on Thu 20 Mar 2008
at 23:58