/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

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