/shoulda_macros/should_be_child_to.rb

http://acts-crummy.googlecode.com/ · Ruby · 31 lines · 27 code · 4 blank · 0 comment · 1 complexity · 04b3c1292d16a2b1e035907f70611f83 MD5 · raw file

  1. module Shoulda
  2. module ActiveRecord
  3. module Matchers
  4. def be_child_to(name)
  5. AssociationMatcher.new(:is_child_to, name)
  6. end
  7. class AssociationMatcher
  8. protected
  9. alias_method :old_macro_description, :macro_description
  10. def macro_description
  11. @macro.to_s == 'is_child_to' ? 'be child to' : old_macro_description
  12. end
  13. end
  14. end
  15. module Macros
  16. def should_be_child_to(association)
  17. matcher = be_child_to(association)
  18. should matcher.description do
  19. mock_parent = mock
  20. mock_inst = subject.class.new
  21. mock_inst.expects(association).with.returns(mock_parent)
  22. assert_same mock_parent, mock_inst.parent
  23. end
  24. end
  25. end
  26. end
  27. end