Skip to content

Instantly share code, notes, and snippets.

@superlou
Created April 22, 2012 22:07
Show Gist options
  • Save superlou/2467223 to your computer and use it in GitHub Desktop.
Save superlou/2467223 to your computer and use it in GitHub Desktop.
Mocking a child object
class DurationRule < ActiveRecord::Base
include RuleMixin
validate :min_or_max_set
validate :min_less_than_max
def satisfied?
true if event.time_span.duration > min_duration
false
end
def min_or_max_set
unless self.min_duration || self.max_duration
errors.add(:base, "Minimum or maximum duration must be set")
end
end
def min_less_than_max
if self.min_duration && self.max_duration
errors.add(:min_duration, "must be less than 'at most'") if self.min_duration > self.max_duration
end
end
end
require 'test_helper'
class DurationRuleTest < ActiveSupport::TestCase
should have_one(:event).through(:rule_assignment)
should "not save if no minimum or maximum duration set" do
refute DurationRule.new.save
end
should "save if minimum duration set" do
assert DurationRule.new(:min_duration => 10).save
end
should "save if maximum duration set" do
assert DurationRule.new(:max_duration => 10).save
end
should "save if minimum and maximum duration set" do
assert DurationRule.new(:min_duration => 10, :max_duration => 10).save
end
should "not save if maximum duration is less than minimum duration" do
refute DurationRule.new(:min_duration => 20, :max_duration => 10).save
end
context "with a DurationRule assigned to an Event" do
setup do
@event = FactoryGirl.create(:event)
@rule = DurationRule.create(:min_duration => 10)
@rule_assignment = RuleAssignment.create(:event => @event, :rule => @rule)
end
should "be satisfied if meets minimum duration" do
@event.time_span = mock(:time_span)
@rule.min_duration = 50
assert_equal true, @rule.satisfied?
end
end
end
class Event < ActiveRecord::Base
has_many :convention_resourceables, :as => :resourceable
has_many :conventions, :through => :convention_resourceables
has_one :time_span, :as => :time_spanable
has_many :rule_assignments, :dependent => :destroy
has_many :rules, :through => :rule_assignments
has_many :be_scheduled_rules, :through => :rule_assignments, :source_type => 'BeScheduledRule'
def rules
rule_assignments.collect {|rule_assignment| rule_assignment.rule}
end
def scheduled?
return true if time_span
false
end
end
module RuleMixin
def self.included(base)
base.has_one :rule_assignment, :as => :rule, :dependent => :destroy
base.has_one :event, :through => :rule_assignment
end
def satisfied?
false
end
def violated?
!satisfied?
end
def activity
rule_assignment.activity
end
def message
""
end
end
ActiveRecord::AssociationTypeMismatch: TimeSpan(#38173560) expected, got Mocha::Mock(#20908400)
/home/lsimons/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.2/lib/active_record/associations/association.rb:204:in `raise_on_type_mismatch'
/home/lsimons/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.2/lib/active_record/associations/has_one_association.rb:8:in `replace'
/home/lsimons/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.2/lib/active_record/associations/singular_association.rb:17:in `writer'
/home/lsimons/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.2/lib/active_record/associations/builder/association.rb:51:in `block in define_writers'
/home/lsimons/RubymineProjects/connie/test/unit/duration_rule_test.rb:34:in `block (2 levels) in <class:DurationRuleTest>'
/home/lsimons/.rvm/gems/ruby-1.9.2-p290/gems/shoulda-context-1.0.0/lib/shoulda/context/context.rb:398:in `call'
/home/lsimons/.rvm/gems/ruby-1.9.2-p290/gems/shoulda-context-1.0.0/lib/shoulda/context/context.rb:398:in `block in create_test_from_should_hash'
/home/lsimons/.rvm/gems/ruby-1.9.2-p290/gems/mocha-0.10.5/lib/mocha/integration/mini_test/version_230_to_262.rb:28:in `run'
/home/lsimons/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.2.2/lib/active_support/testing/setup_and_teardown.rb:35:in `block in run'
/home/lsimons/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.2.2/lib/active_support/callbacks.rb:425:in `_run__2111581593048511830__setup__3309778514694696817__callbacks'
/home/lsimons/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.2.2/lib/active_support/callbacks.rb:405:in `__run_callback'
/home/lsimons/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.2.2/lib/active_support/callbacks.rb:385:in `_run_setup_callbacks'
/home/lsimons/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.2.2/lib/active_support/callbacks.rb:81:in `run_callbacks'
/home/lsimons/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.2.2/lib/active_support/testing/setup_and_teardown.rb:34:in `run'
/home/lsimons/.rvm/gems/ruby-1.9.2-p290/gems/minitest-reporters-0.5.1/lib/minitest/test_runner.rb:21:in `run'
/home/lsimons/.rvm/gems/ruby-1.9.2-p290/gems/minitest-reporters-0.5.1/lib/minitest/suite_runner.rb:68:in `_run_test'
/home/lsimons/.rvm/gems/ruby-1.9.2-p290/gems/minitest-reporters-0.5.1/lib/minitest/suite_runner.rb:55:in `block in _run_suite'
/home/lsimons/.rvm/gems/ruby-1.9.2-p290/gems/minitest-reporters-0.5.1/lib/minitest/suite_runner.rb:55:in `each'
/home/lsimons/.rvm/gems/ruby-1.9.2-p290/gems/minitest-reporters-0.5.1/lib/minitest/suite_runner.rb:55:in `_run_suite'
/home/lsimons/.rvm/gems/ruby-1.9.2-p290/gems/minitest-reporters-0.5.1/lib/minitest/suite_runner.rb:40:in `block (2 levels) in _run_anything'
/home/lsimons/.rvm/gems/ruby-1.9.2-p290/gems/minitest-reporters-0.5.1/lib/minitest/suite_runner.rb:40:in `each'
/home/lsimons/.rvm/gems/ruby-1.9.2-p290/gems/minitest-reporters-0.5.1/lib/minitest/suite_runner.rb:40:in `block in _run_anything'
/home/lsimons/.rvm/gems/ruby-1.9.2-p290/gems/minitest-reporters-0.5.1/lib/minitest/suite_runner.rb:106:in `fix_sync'
/home/lsimons/.rvm/gems/ruby-1.9.2-p290/gems/minitest-reporters-0.5.1/lib/minitest/suite_runner.rb:39:in `_run_anything'
/home/lsimons/.rvm/gems/ruby-1.9.2-p290/gems/minitest-2.11.4/lib/minitest/unit.rb:952:in `run_tests'
/home/lsimons/.rvm/gems/ruby-1.9.2-p290/gems/minitest-2.11.4/lib/minitest/unit.rb:939:in `block in _run'
/home/lsimons/.rvm/gems/ruby-1.9.2-p290/gems/minitest-2.11.4/lib/minitest/unit.rb:938:in `each'
/home/lsimons/.rvm/gems/ruby-1.9.2-p290/gems/minitest-2.11.4/lib/minitest/unit.rb:938:in `_run'
/home/lsimons/.rvm/gems/ruby-1.9.2-p290/gems/minitest-2.11.4/lib/minitest/unit.rb:927:in `run'
/home/lsimons/.rvm/gems/ruby-1.9.2-p290/gems/minitest-2.11.4/lib/minitest/unit.rb:695:in `block in autorun'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment