Module: RSpec::TagMatchers

Defined in:
lib/rspec/tag_matchers.rb,
lib/rspec/tag_matchers/has_tag.rb,
lib/rspec/tag_matchers/has_form.rb,
lib/rspec/tag_matchers/has_input.rb,
lib/rspec/tag_matchers/has_select.rb,
lib/rspec/tag_matchers/has_checkbox.rb,
lib/rspec/tag_matchers/has_date_select.rb,
lib/rspec/tag_matchers/has_time_select.rb,
lib/rspec/tag_matchers/multiple_input_matcher.rb

Defined Under Namespace

Modules: Helpers Classes: HasCheckbox, HasDateSelect, HasForm, HasInput, HasSelect, HasTag, HasTimeSelect, MultipleInputMatcher

Instance Method Summary collapse

Instance Method Details

#have_checkboxHasCheckbox

Matches <input type="checkbox"> tags in HTML.

Examples:

Matching a checked checkbox for terms_of_service

it { should have_checkbox.for(:terms_of_service).checked }

Matching an unchecked checkbox for terms_of_service

it { should have_checkbox.for(:terms_of_service).not_checked }

Returns:

See Also:

Modifiers:

  • checked

    Specifies that the checkbox must be checked.

  • not_checked

    Specifies that the checkbox must not be checked.



20
21
22
# File 'lib/rspec/tag_matchers/has_checkbox.rb', line 20

def have_checkbox
  HasCheckbox.new
end

#have_date_selectHasDateSelect

Matches inputs generated by Rails’ date_select helper.

Examples:

Matching a date select for an event’s start_date

it { should have_date_select.for(:event => :start_date) }

Matching a date select for a credit card expiration

it { should have_date_select.discard(:day).for(:credit_card => :expiration) }

Returns:

See Also:

Modifiers:

  • discard

    Specifies that one or more components of the date are discarded in the date_select helper. For example, discard(:day) should match the output of a date_select helper that was given the option :discard_day => true. Note that this changes the matching criteria to expect a hidden input instead of a <select> element for the components specified.



21
22
23
# File 'lib/rspec/tag_matchers/has_date_select.rb', line 21

def have_date_select
  HasDateSelect.new
end

#have_formHasForm

Matches <form> tags in HTML.

Examples:

Matching a simple form

it { should have_form.with_verb(:post).with_action("/signup") }

Matching a form with an overridden method

it { should have_form.with_verb(:put) }

Returns:

See Also:

Modifiers:

  • with_verb

    Adds a criteria that the form must have the given HTTP verb.

  • with_action

    Adds a criteria that the form must target the given URL as its action.



20
21
22
# File 'lib/rspec/tag_matchers/has_form.rb', line 20

def have_form
  HasForm.new
end

#have_inputHasInput

Matches <input> tags in HTML.

Examples:

Matching an input for the name attribute on user

it { should have_input.for(:user => :name) }
it { should have_input.for(:user, :name) }

Matching an input with a value of "42".

it { should have_input.value("42") }

Returns:

See Also:

Modifiers:

  • for

    Adds a criteria that the input must be for the given attribute.

  • value

    Adds a criteria that the input must have a given value.



22
23
24
# File 'lib/rspec/tag_matchers/has_input.rb', line 22

def have_input
  HasInput.new
end

#have_selectHasSelect

Matches <select> tags in HTML.

Examples:

Matching a select tag for a user’s role

it { should have_select.for(:user => :role) }

Returns:



8
9
10
# File 'lib/rspec/tag_matchers/has_select.rb', line 8

def have_select
  HasSelect.new
end

#have_tag(name) ⇒ HasTag

Matches HTML tags by name.

Examples:

Matching anchor tags

it { should have_tag(:a) }

Matching contents of a tag

it { should have_tag(:a).with_content("my site") }

Matching anchor tags that link to “/”

it { should have_tag(:a).with_attribute(:href => "/") }

Matching anchor tags with an even id attribute

it { should have_tag(:a).with_criteria { |elem| elem[:id].to_i.even? } }

Returns:

See Also:

Modifiers:

  • with_attribute

    Adds a criteria that an element must match the given attributes.

  • with_content

    Adds a criteria that an element’s content must match the given content.

  • with_count

    Adds a criteria that the element must be found a certain number of times.

  • with_criteria

    Adds an arbitrary criteria.



34
35
36
# File 'lib/rspec/tag_matchers/has_tag.rb', line 34

def have_tag(name)
  HasTag.new(name)
end

#have_time_selectHasTimeSelect

Matches inputs generated by Rails’ time_select helper.

Examples:

Matching a time select for an event’s start_time

it { should have_time_select.for(:event => :start_time) }

Returns:



10
11
12
# File 'lib/rspec/tag_matchers/has_time_select.rb', line 10

def have_time_select
  HasTimeSelect.new
end