Class: RSpec::TagMatchers::HasInput
- 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
Instance Method Summary collapse
-
#for(*args) ⇒ HasInput
Adds a criteria that the input tag should be for a given attribute.
-
#initialize(name = :input) ⇒ HasInput
constructor
Initializes a HasInput matcher that matches elements named
name
. -
#value(value) ⇒ HasInput
Adds a criteria that the input’s value must match a certain value.
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
Constructor Details
#initialize(name = :input) ⇒ HasInput
Initializes a HasInput matcher that matches elements named name
.
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) }
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.
102 103 104 |
# File 'lib/rspec/tag_matchers/has_input.rb', line 102 def value(value) with_attribute(:value => value) end |