lib/matchers/associations.rb in mongoid-rspec-1.8.1 vs lib/matchers/associations.rb in mongoid-rspec-1.8.2

- old
+ new

@@ -41,10 +41,16 @@ def ordered_by(association_field_name) raise "#{@association[:type].inspect} does not respond to :order" unless [HAS_MANY, HAS_AND_BELONGS_TO_MANY, EMBEDS_MANY].include?(@association[:type]) @association[:order] = association_field_name.to_s @expectation_message << " ordered by #{@association[:order].inspect}" + + if association_field_name.is_a? Origin::Key + @association[:order_operator] = association_field_name.operator + @expectation_message << " #{order_way(@association[:order_operator])}" + end + self end def with_dependent(method_name) @association[:dependent] = method_name @@ -122,10 +128,19 @@ else @positive_result_message = "#{@positive_result_message} ordered by #{metadata.order}" end end + if @association[:order_operator] + if @association[:order_operator] != metadata.order.operator + @negative_result_message = "#{@positive_result_message} #{order_way(@association[:order_operator] * -1)}" + return false + else + @positive_result_message = "#{@positive_result_message} #{order_way(@association[:order_operator])}" + end + end + if @association[:dependent] if @association[:dependent].to_s != metadata.dependent.to_s @negative_result_message = "#{@positive_result_message} which specified dependent as #{metadata.dependent}" return false else @@ -202,9 +217,15 @@ when BELONGS_TO.name (passive ? 'be referenced in' : 'referenced in') else raise "Unknown association type '%s'" % type end + end + + private + + def order_way(operator) + [nil, "ascending", "descending"][operator] end end def embed_one(association_name) HaveAssociationMatcher.new(association_name, EMBEDS_ONE)