Skip to content

tomknig/view-matchers

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

view-matchers

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

Build Status Code Climate Test Coverage

Installation

Gem

Include the gem in your Gemfile:

group :test do
  gem 'view-matchers', '~> 1.0'
end

Matcher inclusion

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

Matchers

view-matchers exposes some useful matchers for view specs. They are individually documented below.

match_table

  • 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

match_form

  • 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 a Proc as argument. Methodnames within the Proc are interpreted as HTML-Tag matchers. The first parameter of these methods matches the name 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

Contribution

I'd love to see your ideas! I do really appreciate pull requests and Github issues. :octocat:

Author

Tom König @TomKnig

Versioning

view-matchers follows Semantic Versioning 2.0 as defined at http://semver.org.

License

view-matchers is available under the MIT license. See the LICENSE file for more info.

About

Expressive RSpec matchers for ascii tables and form fields.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages