Sha256: 6af7214055120ebe748742ca916699ad27b2abff9fc4bd54548c11d62669e3d9

Contents?: true

Size: 1.68 KB

Versions: 2

Compression:

Stored size: 1.68 KB

Contents

RSpec::Matchers.define :have_property do |property|

  chain :of_type do |type|
    @type = type
  end

  match do |model|
    @model_class = model.is_a?(Class) ? model : model.class
    @has_property = @model_class.properties.map(&:name).include? property
    @model_property = @model_class.properties.find{|f| f.name == property}
    if @type
      @has_property && @model_property.class == @type
    else
      @has_property
    end
  end

  failure_message_for_should do |model|
    if @type and @model_property
      type = @model_property.class
      "property #{property} should be of type #{@type} but is of type #{type}"
    else
      "Expected #{@model_class} to have property #{property}"
    end
  end

  failure_message_for_should_not do |model|
    if @type and @model_property
      type = @model_property.class
      "property #{property} should not be of type #{@type} but is of type #{type}"
    else
      "Expected #{@model_class} to not have property #{property}, but it does!"
    end
  end


end


# module DataMapper
#   module Matchers

#     class HaveProperty
#       def initialize(property)
#         @property = property.to_sym
#       end

#       def matches?(model)
#         model_class = model.is_a?(Class) ? model : model.class
#         model_class.properties.map(&:name).include? @property
#       end

#       def failure_message
#         "expected to have property #{@property}"
#       end

#       def negative_failure_message
#         "expected to not have property #{@property}"
#       end
      
#       def description
#         "has property #{@children}"
#       end
#     end


#     def have_property(name)
#       HaveProperty.new(name)
#     end

#   end
# end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dm-rspec2-0.3.0 lib/dm/matchers/have_property.rb
dm-rspec2-0.2.4 lib/dm/matchers/have_property.rb