Skip to content

Instantly share code, notes, and snippets.

@bkaney
Created September 8, 2010 17:36
Show Gist options
  • Save bkaney/570467 to your computer and use it in GitHub Desktop.
Save bkaney/570467 to your computer and use it in GitHub Desktop.
describe Committee do
it { should link_one(:municipality) }
end
Rspec::Matchers.define :link_one do |expected|
match do |actual|
(actual.should include_association(expected)) &&
(actual.should associate_one(expected))
end
end
# "lower-level" matchers
Rspec::Matchers.define :associate_one do |expected|
match do |actual|
actual.class.associations[expected.to_sym].try(:one?)
end
failure_message_for_should do |actual|
"expected #{ actual.class.associations[expected.to_sym].inspect} to associate one"
end
failure_message_for_should_not do |actual|
"expected #{ actual.class.associations[expected.to_sym].inspect} to not associate one"
end
end
Rspec::Matchers.define :include_association do |expected|
match do |actual|
actual.class.associations.keys.map(&:to_sym).include?(expected.to_sym)
end
failure_message_for_should do |actual|
"expected #{actual.class.associations.keys.map(&:to_sym)} to include #{expected.to_sym}"
end
failure_message_for_should_not do |actual|
"expected #{actual.class.associations.keys} to not include #{expected}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment