PageRenderTime 17ms CodeModel.GetById 10ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 1require 'test_helper'
 2require 'has_parent'
 3require 'mocha'
 4
 5class HasParentTest < ActiveSupport::TestCase
 6  def setup
 7    @parent = mock
 8    @child = MockObject.new @parent
 9    @grandchild = MockChild.new @child
10  end
11
12  test 'Class does not get parent method until is_child_to is called on it' do
13    cls = Class.new MockBaseClass
14    parent = @parent
15    cls.send(:define_method, :get_parent) { parent }
16
17    instance = cls.new
18
19    assert_raise(NoMethodError) { instance.parent }
20
21    cls.is_child_to :get_parent
22
23    assert_nothing_raised do
24      assert_same @parent, instance.parent
25    end
26  end
27
28  test '@parent is parent of @child' do
29    assert_same @parent, @child.parent
30    assert_same @child, @grandchild.parent
31  end
32
33  test 'is_child_to throws exception when called more than once' do
34    assert_raise(ArgumentError) { @child.class.is_child_to :something_else }
35  end
36end
37
38class MockBaseClass
39  include ActsCrummy::HasParent
40end
41
42class MockObject < MockBaseClass
43  def initialize par
44    @mock_parent = par
45  end
46
47  is_child_to :mock_parent
48
49  attr_accessor :mock_parent
50end
51
52# need another class to test that the class variable being used gets
53# applied to the inheriting class, not to the class it's going to
54# inherit from (i.e. we want it to belong to the model class, not
55# the parent ActiveRecord::Base)
56class MockChild < MockBaseClass
57  def initialize par
58    @my_mock_parent = par
59  end
60  
61  is_child_to :my_mock_parent
62
63  attr_accessor :my_mock_parent
64end