Sha256: 26558397c5b3a76e37912f162138ee5f692310d126be4d5620667048a9fd1039

Contents?: true

Size: 1.41 KB

Versions: 109

Compression:

Stored size: 1.41 KB

Contents

module BeModelWithValuesMatcher
 
  class BeModelWithValues
    def initialize(expected_hash, compare_as_strings=false)
      @compare_as_strings = compare_as_strings
      @expected_hash = expected_hash.stringify_keys!
    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

109 entries across 109 versions & 1 rubygems

Version Path
fossil-0.5.50 spec/be_model_with_values_matcher.rb
fossil-0.5.49 spec/be_model_with_values_matcher.rb
fossil-0.5.48 spec/be_model_with_values_matcher.rb
fossil-0.5.47 spec/be_model_with_values_matcher.rb
fossil-0.5.46 spec/be_model_with_values_matcher.rb
fossil-0.5.45 spec/be_model_with_values_matcher.rb
fossil-0.5.44 spec/be_model_with_values_matcher.rb
fossil-0.5.43 spec/be_model_with_values_matcher.rb
fossil-0.5.42 spec/be_model_with_values_matcher.rb
fossil-0.5.41 spec/be_model_with_values_matcher.rb
fossil-0.5.40 spec/be_model_with_values_matcher.rb
fossil-0.5.39 spec/be_model_with_values_matcher.rb
fossil-0.5.38 spec/be_model_with_values_matcher.rb
fossil-0.5.37 spec/be_model_with_values_matcher.rb
fossil-0.5.36 spec/be_model_with_values_matcher.rb
fossil-0.5.35 spec/be_model_with_values_matcher.rb
fossil-0.5.34 spec/be_model_with_values_matcher.rb
fossil-0.5.33 spec/be_model_with_values_matcher.rb
fossil-0.5.32 spec/be_model_with_values_matcher.rb
fossil-0.5.31 spec/be_model_with_values_matcher.rb