Class: RSpec::TagMatchers::HasDateSelect

Inherits:
MultipleInputMatcher show all
Includes:
RSpec::TagMatchers::Helpers::SentenceHelper
Defined in:
lib/rspec/tag_matchers/has_date_select.rb

Overview

A matcher that matches Rails' date_select drop-downs.

Instance Method Summary (collapse)

Methods included from RSpec::TagMatchers::Helpers::SentenceHelper

#make_sentence

Methods inherited from MultipleInputMatcher

#for, #matches?

Constructor Details

- (HasDateSelect) initialize

Initializes a HasDateSelect matcher.



30
31
32
# File 'lib/rspec/tag_matchers/has_date_select.rb', line 30

def initialize
  super('1i' => HasSelect.new, '2i' => HasSelect.new, '3i' => HasSelect.new)
end

Instance Method Details

- (String) description

Returns a description of the matcher's criteria.

Returns:

  • (String)


76
77
78
# File 'lib/rspec/tag_matchers/has_date_select.rb', line 76

def description
  [basic_description, extra_description].compact.reject(&:empty?).join(" ")
end

- (HasDateSelect) discard(*parts)

Note:

This modifier replaces the matchers for the given date components. Any modifiers that were called before this one will be lost for those matchers. Therefor, discard should be called before other modifiers, such as for. For example, the following matcher will only test the input names for the month and day components.

it { should have_date_select.for(:start_date).discard(:year) }

In order to test the input names for all three components, the for modifier must come after the discard modifier:

it { should have_date_select.discard(:year).for(:start_date) }

Specifies that one or more components of the date will be hidden by Rails' date_select helper. For example, calling date_select with the option :discard_day => true produces two <select> elments (for the year and month components) and one hidden input for the date component:

<select name="model[date(1i)]">...</select>
<select name="model[date(2i)]">...</select>
<input type="hidden" name="model[date(3i)]" />

Since the default is to match three <select> elements, one must call discard(:day) to tell a HasDateSelect matcher to expect a hidden input for the third component.

Parameters:

  • *parts (Symbols)

    A variable number of symbols specifying which components should be discarded. Valid symbols are :year, :month, and :day.

Returns:



62
63
64
65
66
67
68
69
70
71
# File 'lib/rspec/tag_matchers/has_date_select.rb', line 62

def discard(*parts)
  @discard ||= []
  @discard  += parts

  parts.each do |part|
    # TODO: create a HasHiddenInput matcher
    replace_matcher(part, HasInput.new.with_attribute(:type => :hidden))
  end
  self
end

- (String) failure_message

Returns an explanation of why the matcher failed to match with should.

Returns:

  • (String)


83
84
85
# File 'lib/rspec/tag_matchers/has_date_select.rb', line 83

def failure_message
  "expected document to #{description}; got: #{@rendered}"
end

- (String) negative_failure_message

Returns an explanation of why the matcher failed to match with should_not.

Returns:

  • (String)


90
91
92
# File 'lib/rspec/tag_matchers/has_date_select.rb', line 90

def negative_failure_message
  "expected document to not #{description}; got: #{@rendered}"
end