Sha256: f45a64643ddd503570c190bf535de6db825a5626df315088b2bed03488738ff0

Contents?: true

Size: 1.2 KB

Versions: 4

Compression:

Stored size: 1.2 KB

Contents

module ElabsMatchers
  module Matchers
    module Persist
      rspec :type => :model

      class PersistMatcher < Struct.new(:attribute, :value)
        attr_reader :record

        def matches?(record)
          record.send(:"#{attribute}=", value)
          record.save!
          record = record.class.find(record.id)
          @final_value = record.send(attribute)

          @final_value == value
        end

        def failure_message_for_should
          "Expected #{attribute} to be persisted and retain its value of #{value.inspect} but the value was #{@final_value.inspect}."
        end

        def failure_message_for_should_not
          "Expected #{attribute} not to be persisted and retain its value of #{value.inspect} but it did."
        end
      end

      ##
      #
      # Asserts if an assigned value persistes in the database.
      #
      # @param [Symbol] attribute       The name of the attibute to check if it persisted or not
      # @param [Object] value           The value to persist to the attribute
      #
      # Example:
      # post.should persist(:title, "Blog post")

      def persist(attribute, value)
        PersistMatcher.new(attribute, value)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
elabs_matchers-0.0.7 lib/elabs_matchers/matchers/persist.rb
elabs_matchers-0.0.6 lib/elabs_matchers/matchers/persist.rb
elabs_matchers-0.0.5 lib/elabs_matchers/matchers/persist.rb
elabs_matchers-0.0.4 lib/elabs_matchers/matchers/persist.rb