Class: RSpec::TagMatchers::HasInput

Inherits:
HasTag
  • Object
show all
Defined in:
lib/rspec/tag_matchers/has_input.rb

Overview

A base class for form input matchers.

Subclassing

HasInput is intended to be subclassed to create more expressive matchers for specific types of form inputs. The helper methods in HasInput, e.g., #for, are intended to be useful for all types of form inputs.

By default, HasInput matches <input> elements, but this can be overridden by subclasses, e.g., HasSelect matches <select> elements. This can be done in the matcher’s constructor:

class HasSelect < HasInput
  def initialize
    super(:select)
  end
end

Some matchers might want to add more criteria inside their constructors. For example, matchers that match specific types of <input> tags will want to add a criteria for matching the type attribute:

class HasCheckbox < HasInput
  def initialize
    super(:checkbox)
    with_attribute(:type => :checkbox)
  end
end

Direct Known Subclasses

HasCheckbox, HasSelect

Instance Method Summary collapse

Methods inherited from HasTag

#description, #failure_message, #matches?, #negative_failure_message, #test_attribute, #with_attribute, #with_content, #with_count, #with_criteria

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

#make_sentence

Constructor Details

#initialize(name = :input) ⇒ HasInput

Initializes a HasInput matcher that matches elements named name.

Parameters:

  • name (Symbol) (defaults to: :input)

    The type of HTML tag to match.



60
61
62
# File 'lib/rspec/tag_matchers/has_input.rb', line 60

def initialize(name = :input)
  super
end

Instance Method Details

#for(*args) ⇒ HasInput

Adds a criteria that the input tag should be for a given attribute.

HasInput provides the #for modifier to all of its subclasses, which is useful for matching the input’s name with Rails conventions. For example, a Rails template that uses form_for might output HTML that looks like this:

<form method="POST" action="/users">
  <input type="text" name="user[name]" />
</form>

Instead of writing:

it { should have_input.with_attribute(:name => "user[name]") }

the user can write a more concise spec using #for:

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

Examples:

Match an input for the name attribute of user

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

Match an input for a nested attribute

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

Parameters:

  • args (Array, Hash)

    A hierarchy of strings that specifiy the attribute name.

Returns:



92
93
94
95
# File 'lib/rspec/tag_matchers/has_input.rb', line 92

def for(*args)
  with_attribute(:name => build_name(*args))
  self
end

#value(value) ⇒ HasInput

Adds a criteria that the input’s value must match a certain value.

Parameters:

  • value (String, Symbol, Regexp, true, false)

Returns:



102
103
104
# File 'lib/rspec/tag_matchers/has_input.rb', line 102

def value(value)
  with_attribute(:value => value)
end