/test/has_parent_test.rb
http://acts-crummy.googlecode.com/ · Ruby · 64 lines · 45 code · 15 blank · 4 comment · 0 complexity · 179f67e0a8c0cc0f43b402d6977df844 MD5 · raw file
- require 'test_helper'
- require 'has_parent'
- require 'mocha'
-
- class HasParentTest < ActiveSupport::TestCase
- def setup
- @parent = mock
- @child = MockObject.new @parent
- @grandchild = MockChild.new @child
- end
-
- test 'Class does not get parent method until is_child_to is called on it' do
- cls = Class.new MockBaseClass
- parent = @parent
- cls.send(:define_method, :get_parent) { parent }
-
- instance = cls.new
-
- assert_raise(NoMethodError) { instance.parent }
-
- cls.is_child_to :get_parent
-
- assert_nothing_raised do
- assert_same @parent, instance.parent
- end
- end
-
- test '@parent is parent of @child' do
- assert_same @parent, @child.parent
- assert_same @child, @grandchild.parent
- end
-
- test 'is_child_to throws exception when called more than once' do
- assert_raise(ArgumentError) { @child.class.is_child_to :something_else }
- end
- end
-
- class MockBaseClass
- include ActsCrummy::HasParent
- end
-
- class MockObject < MockBaseClass
- def initialize par
- @mock_parent = par
- end
-
- is_child_to :mock_parent
-
- attr_accessor :mock_parent
- end
-
- # need another class to test that the class variable being used gets
- # applied to the inheriting class, not to the class it's going to
- # inherit from (i.e. we want it to belong to the model class, not
- # the parent ActiveRecord::Base)
- class MockChild < MockBaseClass
- def initialize par
- @my_mock_parent = par
- end
-
- is_child_to :my_mock_parent
-
- attr_accessor :my_mock_parent
- end