Module: Lafcadio::DomainMock

Defined in:
lib/lafcadio/test.rb

Overview

A convenience module for test-cases of Lafcadio-dependent applications. Include this module in a test-case, and you automatically get the class-level method setup_mock_dobjs. This calls DomainObject.default_mock, and assigns the result to an instance variable named after the domain class. Note that if your test case also defines a setup, you should make sure to call super in that setup method to make setup_mock_dobjs work.

class User < Lafcadio::DomainObject
  strings :fname, :lname, :email
end

class TestSendMessage < Test::Unit::TestCase
  include Lafcadio::DomainMock
  setup_mock_dobjs User
  def test_send_to_self
    SendMessage.new( 'sender' => @user, 'recipient' => @user )
    assert_equal( 1, Message.all.size )
  end
end

setup_mock_dobjs can handle plural domain classes:

setup_mock_dobjs User, Message

It can also handle assignments to different instance variables:

setup_mock_dobjs User, '@sender'

Defined Under Namespace

Classes: DomainClassSymbolMapper

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/lafcadio/test.rb', line 113

def method_missing( sym, *args )
	method_name = sym.id2name
	if method_name =~ /^custom_mock_(.*)/
		domain_class = Module.by_name( $1.underscore_to_camel_case )
		commit_domain_object( domain_class, *args )
	elsif method_name =~ /^default_mock_(.*)/
		Module.by_name( $1.underscore_to_camel_case ).default_mock
	else
		super
	end
end

Class Method Details

.included(includer) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/lafcadio/test.rb', line 80

def self.included( includer )
	def includer.setup_mock_dobjs( *domain_classes_or_symbol_names )
		domain_classes = DomainClassSymbolMapper.new
		domain_classes_or_symbol_names.each { |domain_class_or_symbol_name|
			domain_classes.process( domain_class_or_symbol_name )
		}
		domain_classes.finish
		domain_classes.each { |my_domain_class, my_symbol_name|
			proc = Proc.new { |test_case|
				test_case.instance_variable_set(
					my_symbol_name, my_domain_class.default_mock
				)
			}
			setup_procs << proc
		}
	end

	def includer.setup_procs
		unless defined? @@all_setup_procs
			@@all_setup_procs = Hash.new { |hash, domain_class|
				hash[domain_class] = []
			}
		end
		@@all_setup_procs[self]
	end
end

Instance Method Details

#commit_domain_object(domain_class, non_default_values = {}) ⇒ Object



107
108
109
110
111
# File 'lib/lafcadio/test.rb', line 107

def commit_domain_object( domain_class, non_default_values = {} )
	self.class.mock_domain_files.each do |file| require file; end
	dobj = domain_class.send( :custom_mock, non_default_values )
	dobj.commit
end

#setupObject



125
126
127
# File 'lib/lafcadio/test.rb', line 125

def setup
	self.class.setup_procs.each { |proc| proc.call( self ) }
end