Created
September 25, 2012 12:46
-
-
Save mattsnyder/3781560 to your computer and use it in GitHub Desktop.
Custom matchers for testing Ripple Documents
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
RSpec::Matchers.define :be_a_ripple_document do | |
match do |doc| | |
doc.class.included_modules.include?(Ripple::Document) || | |
doc.class.included_modules.include?(Ripple::EmbeddedDocument) | |
end | |
failure_message_for_should do | |
"#{actual.class.to_s} should be a Ripple::Document" | |
end | |
description do | |
"be a Ripple document" | |
end | |
end | |
RSpec::Matchers.define :be_embedded_in do |parent| | |
match do |doc| | |
doc.class.included_modules.include?(Ripple::EmbeddedDocument) && | |
classify(parent).embedded_associations.select{|association| association.class_name == doc.class.to_s }.size > 0 | |
end | |
failure_message_for_should do | |
"#{actual.class.to_s} should be embedded in #{classify(parent)}" | |
end | |
def classify(symbol) | |
symbol.to_s.titleize.constantize | |
end | |
description do | |
"be embedded in #{classify(parent)}" | |
end | |
end | |
RSpec::Matchers.define :be_timestamped do | |
match do |doc| | |
doc.class.properties.include?(:created_at) && | |
doc.class.properties.include?(:updated_at) && | |
doc.respond_to?(:touch) | |
end | |
failure_message_for_should do | |
"should be timestamped" | |
end | |
failure_message_for_should_not do | |
"should not be timestamped" | |
end | |
description do | |
"be a Timestamped Document" | |
end | |
end | |
RSpec::Matchers.define :have_property do |property| | |
match do |doc| | |
doc.class.properties.include? property | |
end | |
failure_message_for_should do | |
"#{actual.class.to_s} should have property #{property}" | |
end | |
description do | |
"have a #{property} property" | |
end | |
end | |
RSpec::Matchers.define :have_many do |association_name| | |
match do |doc| | |
doc.methods.map(&:to_sym).include?(association_name) && | |
doc.methods.map(&:to_sym).include?("#{association_name.to_s}=".to_sym) && | |
doc.class.linked_associations.select{|association| association.name == association_name}.size > 0 | |
end | |
failure_message_for_should do | |
"#{actual.class.to_s} should have many #{association_name}" | |
end | |
description do | |
"have many #{association_name.to_s.gsub '_',' ' }" | |
end | |
end | |
RSpec::Matchers.define :have_one do |association_name| | |
match do |doc| | |
doc.methods.map(&:to_sym).include?(association_name) && | |
doc.methods.map(&:to_sym).include?("#{association_name.to_s}=".to_sym) && | |
doc.methods.map(&:to_sym).include?("#{association_name.to_s}?".to_sym) | |
end | |
failure_message_for_should do | |
"#{actual.class.to_s} should have one #{association_name}" | |
end | |
description do | |
"have one #{association_name.to_s.gsub '_',' ' }" | |
end | |
end | |
RSpec::Matchers.define :embed_many do |association_name| | |
match do |doc| | |
doc.methods.map(&:to_sym).include?(association_name) && | |
doc.methods.map(&:to_sym).include?("#{association_name.to_s}=".to_sym) && | |
doc.class.embedded_associations.select{|association| association.name == association_name}.size > 0 | |
end | |
failure_message_for_should do | |
"#{actual.class.to_s} should embed many #{association_name}" | |
end | |
description do | |
"embed many #{association_name.to_s.gsub '_',' ' }" | |
end | |
end | |
RSpec::Matchers.define :index do |index_name| | |
match do |doc| | |
doc.class.indexes.has_key?(index_name) | |
end | |
failure_message_for_should do | |
"#{actual.class.to_s} should have a secondary index for #{index_name}" | |
end | |
description do | |
"define #{index_name} as a secondary index." | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment