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



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/lafcadio/test.rb', line 80

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



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/lafcadio/test.rb', line 47

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



74
75
76
77
78
# File 'lib/lafcadio/test.rb', line 74

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



92
93
94
# File 'lib/lafcadio/test.rb', line 92

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