Sha256: e34e8a29add76b7725df022a9da93d3dff843f047bcf72fafca40982ef9c0dbd

Contents?: true

Size: 1.62 KB

Versions: 5

Compression:

Stored size: 1.62 KB

Contents

require "warp/model_matchers/matcher"

module Warp
  module ModelMatchers
    class AssociationMatcher < Warp::ModelMatchers::Matcher
      attr_reader :expected_macro, :key
      
      def initialize(expected_macro, key)
        @expected_macro = expected_macro
        @key = key
      end

      def matches?(model_or_instance)
        @model_or_instance = model_or_instance

        association && values_match?(expected_macro, assocation_macro)
      end

      def description
        "have a #{expected_macro} association with :#{key}"
      end

      def failure_message
        if association
          "expected #{model_name} to #{description}, but had a #{assocation_macro} association with :#{key}"
        else
          "expected #{model_name} to #{description}"
        end
      end

      def failure_message_when_negated
        "expected #{model_name} to not #{description}"
      end

      private

      def association
        model.reflect_on_association(key)
      end

      def assocation_macro
        association.macro
      end
    end

    def have_many(key)
      AssociationMatcher.new(:has_many, key)
    end

    def have_one(key)
      AssociationMatcher.new(:has_one, key)
    end

    def belong_to(key)
      AssociationMatcher.new(:belongs_to, key)
    end

    def have_and_belong_to_many(key)
      if ActiveRecord::VERSION::STRING[0] == "4" && ActiveRecord::VERSION::STRING[3] != "0"
        raise NotImplementedError, "In Rail 4.1+ the has_and_belongs_to_many helper produces a has_many :through association."
      else
        AssociationMatcher.new(:has_and_belongs_to_many, key)
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
warp-1.4.0 lib/warp/model_matchers/association_matcher.rb
warp-1.3.3 lib/warp/model_matchers/association_matcher.rb
warp-1.3.2 lib/warp/model_matchers/association_matcher.rb
warp-1.3.1 lib/warp/model_matchers/association_matcher.rb
warp-1.3.0 lib/warp/model_matchers/association_matcher.rb