Expressive RSpec matchers for ascii tables and form fields.
- match_table: write ascii tables and match their existence
- match_form: match form fields with an expressive dsl
Include the gem in your Gemfile:
group :test do
gem 'view-matchers', '~> 1.0'
end
You can either include the matchers in a particular spec:
describe 'a particular view with tables' do
include ViewMatchers
# ...
end
... or include the matchers globally in a spec_helper.rb
file:
require 'view_matchers'
RSpec.configure do |config|
config.include ViewMatchers
end
view-matchers exposes some useful matchers for view specs. They are individually documented below.
- You can match a table in a rendered view within a spec with
match_table
. - Matches are partial. The actually rendered table can - but does not have to - be a superset of the expected table to evaluate the expectation to true.
# spec/views/table_spec.rb
it 'renders all products in a proper table' do
expect(rendered).to match_table %(
+----------+----------+----------+
| Number | Product | Price |
+----------+----------+----------+
| 1 | Computer | 42,00 € |
+----------+----------+----------+
| 2 | Phone | 21,00 € |
+----------+----------+----------+
)
end
- You can match a table in a rendered view within a spec with
match_form
. - Matches are partial. The actually rendered form can - but does not have to - be a superset of the expected form to evaluate the expectation to true.
match_form
takes aProc
as argument. Methodnames within theProc
are interpreted as HTML-Tag matchers. The first parameter of these methods matches thename
attribute, which can be followed by arbitrary attribute names and values. The last parameter is an optional block, that can be provided to match nested tags.
# spec/views/sign_up_spec.rb
it 'renders a sign up form' do
expect(rendered).to match_form proc {
input 'user[email]', type: 'email'
input 'user[password]', type: 'password'
input 'user[password_confirmation]', type: 'password'
select 'user[language]' do
option nil, value: 'Objective-C'
option nil, value: 'Swift'
end
}
end
I'd love to see your ideas! I do really appreciate pull requests and Github issues.
view-matchers follows Semantic Versioning 2.0 as defined at http://semver.org.
view-matchers is available under the MIT license. See the LICENSE file for more info.