lib/matchers/associations/associations.rb in mongoid-minitest-0.1.1 vs lib/matchers/associations/associations.rb in mongoid-minitest-0.1.2
- old
+ new
@@ -7,13 +7,11 @@
BELONGS_TO = Mongoid::Relations::Referenced::In
EMBEDS_ONE = Mongoid::Relations::Embedded::One
EMBEDS_MANY = Mongoid::Relations::Embedded::Many
EMBEDDED_IN = Mongoid::Relations::Embedded::In
- class HaveAssociationMatcher
- include Helpers
-
+ class HaveAssociationMatcher < Matcher
def initialize(name, type)
@association = {}
@association[:name] = name.to_s
@association[:type] = type
@description = "#{type_description} #{@association[:name].inspect}"
@@ -23,18 +21,25 @@
@association[:class] = klass
@description << " of type #{@association[:class].inspect}"
self
end
+ def as_inverse_of(inverse_of)
+ @association[:inverse_of] = inverse_of
+ @description << " as the inverse of #{@association[:inverse_of].inspect}"
+ self
+ end
+
def matches?(subject)
@klass = class_of(subject)
@metadata = @klass.relations[@association[:name]]
@result = true
check_association_name
check_association_type
check_association_class if @association[:class]
+ check_association_inverse_of if @association[:inverse_of]
@result
end
def failure_message
@@ -77,9 +82,18 @@
if @association[:class] != @metadata.klass
@negative_message = "#{@positive_message} of type #{@metadata.klass}"
@result = false
else
@positive_message << " of type #{@metadata.klass}" if @association[:class]
+ end
+ end
+
+ def check_association_inverse_of
+ if @association[:inverse_of] != @metadata.inverse_of
+ @negative_message = "..."
+ @result = false
+ else
+ @positive_message << " as inverse of #{@metadata.inverse_of}" if @association[:inverse_of]
end
end
def type_description(type = nil, passive = true)
type ||= @association[:type]