lib/matchers/document/have_field.rb in mongoid-minitest-0.0.1 vs lib/matchers/document/have_field.rb in mongoid-minitest-0.0.2

- old
+ new

@@ -1,10 +1,10 @@ module Mongoid module Matchers module Document class HaveFieldMatcher - include Mongoid::Matchers::Helpers + include Helpers def initialize(*fields) @fields = fields.collect(&:to_s) end @@ -16,24 +16,24 @@ def with_default_value(default) @default = default self end - def matches?(klass) - @klass = klass.is_a?(Class) ? klass : klass.class + def matches?(subject) + @klass = class_of(subject) @errors = [] @fields.each do |field| if @klass.fields.include?(field) error = "" result_field = @klass.fields[field] - if @type && result_field.type != @type + if check_type_with(result_field.type) error << " of type #{result_field.type.inspect}" end - if !@default.nil? && !result_field.default.nil? && result_field.default != @default + if check_default_with(result_field.default) error << " with default value of #{result_field.default.inspect}" end @errors << "field #{field.inspect << error}" if !error.blank? else @@ -43,23 +43,33 @@ @errors.empty? end def failure_message - "Expected #{@klass.inspect} to #{description}, got #{@errors.to_sentence}" + "#{@klass} to #{description}, got #{@errors.to_sentence}" end def negative_failure_message - msg = "Expected #{@klass.inspect} to not #{description}, " + msg = "#{@klass.inspect} to not #{description}, " msg << "got #{@klass.inspect} to #{description}" end def description - desc = "have #{@fields.size > 1 ? 'fields' : 'field'} named" + desc = "have #{@fields.size > 1 ? 'fields' : 'field'} named" desc << " #{to_sentence(@fields)}" desc << " of type #{@type.inspect}" if @type desc << " with default value of #{@default.inspect}" if !@default.nil? desc + end + + private + + def check_type_with(type) + @type && type != @type + end + + def check_default_with(default) + !@default.nil? && !default.nil? && default != @default end end def have_field(*fields) HaveFieldMatcher.new(*fields)