module Remarkable::Mongoid module Matchers # Specify the document should have a field # # examples: # it { should have_field :name } # it { should have_field :age, :type => Integer, :default => 0 } # # @param [Symbol, Hash] options # @option options [Class] :type The data type # @option options :default The default value for this field # # @return [Remarkable::Mongoid::Matchers::HasFieldMatcher] def have_field(field, options = {}) HasFieldMatcher.new(field, options) end class HasFieldMatcher attr_accessor :field, :options def initialize(field, options) self.field = field.to_s self.options = { :type => Object }.merge(options) end def matches?(subject) @subject = subject.is_a?(Class) ? subject : subject.class @subject.fields.has_key?(field) && (@subject.fields[field].type == options[:type]) end def description "have field #{field} #{humanized_options}" end def failure_message_for_should "expected #{@subject} to have field #{field} #{humanized_options}" end def failure_message_for_should_not "expected #{@subject} to not have field #{field} #{humanized_options}" end private def humanized_options "of type #{options[:type]}" end end end end