Sha256: d26a563ec6651a38aad18514ec4751ce4d225092e21a585b3bbb0092fc76418b

Contents?: true

Size: 1011 Bytes

Versions: 2

Compression:

Stored size: 1011 Bytes

Contents

module SimpleShipping
  module CustomMatchers
    class ValidateSubmodelMatcher < BasicMatcher
      def initialize(attribute)
        @attribute = attribute.to_sym
      end
      
      def matches?(model)
        @model = model

        @model.send("#{@attribute}=", Class.new.new)
        return false unless has_error?

        submodel = @class.new
        submodel.stub!(:valid? => false)
        @model.send("#{@attribute}=", submodel)
        return false unless has_error?

        submodel.stub!(:valid? => true)
        @model.send("#{@attribute}=", submodel)
        !has_error?
      end

      def as(klass)
        @class = klass
        self
      end

      def description
        "validate submodel #{@attribute.inspect} as instance of #{@class}"
      end

      def has_error?
        @model.valid?
        !!@model.errors.messages[@attribute]
      end
      private :has_error?
    end

    def validate_submodel(attr_name)
      ValidateSubmodelMatcher.new(attr_name)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
simple_shipping-0.4.7 spec/support/custom_matchers/validate_submodel_matcher.rb
simple_shipping-0.4.6 spec/support/custom_matchers/validate_submodel_matcher.rb