Sha256: f17a888309f41d944fdc4f81dc31bc014983ee135006c870b3329b251eba0409

Contents?: true

Size: 1.41 KB

Versions: 2

Compression:

Stored size: 1.41 KB

Contents

module ElabsMatchers
  module Matchers
    module HaveFields
      rspec :type => :request
      rspec :type => :feature

      class HaveFieldsMatcher < Struct.new(:fields)
        attr_reader :page

        def matches?(page)
          @page = page
          fields.all? { |label, value| page.has_field?(label, :with => value) }
        end

        def does_not_match?(page)
          @page = page
          fields.all? { |label, value| page.has_no_field?(label, :with => value) }
        end

        def failure_message
          field_values = page.all("input, textarea").map { |input| input[:value] }

          "expected page to have the fields #{fields.inspect}, but it didn't.
           The fields on the page had the following values: #{field_values.to_sentence}."
        end
        alias_method :failure_message_for_should, :failure_message

        def failure_message_when_negated
          "expected page not to have the fields #{fields.inspect}, but it did."
        end
        alias_method :failure_message_for_should_not, :failure_message_when_negated
      end

      ##
      #
      # Asserts if the supplied fields exists or not
      #
      # @param [Hash] fields         A hash containing pairs of field name => value
      #
      # Example:
      # page.should have_fields("Author" => "Adam", "Year" => "2011")

      def have_fields(fields)
        HaveFieldsMatcher.new(fields)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
elabs_matchers-1.0.1 lib/elabs_matchers/matchers/have_fields.rb
elabs_matchers-1.0.0 lib/elabs_matchers/matchers/have_fields.rb