Sha256: 359f51fadfc7e2c29e52a5f727241f8b993089ba81593e78777d1c620591851e

Contents?: true

Size: 1.39 KB

Versions: 3

Compression:

Stored size: 1.39 KB

Contents

module BeModelWithValuesMatcher
 
  class BeModelWithValues
    def initialize(expected_hash, compare_as_strings=false)
      @compare_as_strings = compare_as_strings
      @expected_hash = expected_hash
    end

    def matches?(model)
      @model_hash = model.fill_hash(@expected_hash.keys).stringify_keys!
      @diff_keys = @expected_hash.keys - @model_hash.keys
      @diff_act_values = {}
      @diff_exp_values = {}
      return false unless @diff_keys.empty?
      @model_hash.each do |k,v|
        model_value = @compare_as_strings ? @model_hash[k].to_s : @model_hash[k]
        expected_value = @compare_as_strings ? @expected_hash[k].to_s : @expected_hash[k]
        unless model_value == expected_value
          @diff_act_values[k] = v
          @diff_exp_values[k] = @expected_hash[k]
        end
      end
      return @diff_act_values.empty?
    end

    def failure_message
      str = "expected:\n#{@expected_hash.inspect}\n but got:\n#{@model_hash.inspect}"
      str += "\n\nmissing keys: #{@diff_keys.inspect}" unless @diff_keys.empty?
      unless @diff_act_values.empty?
        str += "\n\nunequal values: \nexpected: #{@diff_exp_values.inspect} \n but got:\n#{@diff_act_values.inspect}"
      end
      str
    end
  end

  def be_model_with_values(expected)
    BeModelWithValues.new(expected)
  end

  def be_model_with_values_as_strings(expected)
    BeModelWithValues.new(expected,true)
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
fossil-0.3.14 spec/be_model_with_values_matcher.rb
fossil-0.3.13 spec/be_model_with_values_matcher.rb
fossil-0.3.12 spec/be_model_with_values_matcher.rb