Skip to content

davydovanton/rspec-hanami

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

65 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

RSpec::Hanami

Build Status Coverage Status

rspec-hanami is a testing framework for hanami

Installation

Add this line to your application's Gemfile:

group :test do
  gem 'rspec-hanami'
end

And then execute:

$ bundle

Or install it yourself as:

$ gem install rspec-hanami

After that require gem to spec_helper.rb and include matchers to rspec:

require 'rspec/hanami'

RSpec.configure do |config|
  config.include RSpec::Hanami::Matchers

  # ...
end

Capybara

Check your spec/features_helper.rb and spec/support/Capybara.rb files. If you find something like this:

Capybara.app = Hanami::Container.new
# or
Capybara.app = Hanami::App.new

Please change this line to:

Capybara.app = ::Hanami::Container.new
# or
Capybara.app = ::Hanami::App.new

For more information see this issue

Supported matchers

Request helpers

You can use familiar request helpers like #get, #post, etc. These methods make full hanami app request and return env (array with 3 elements).

For using these helpers include RSpec::Hanami::RequestHelpers to your spec_helper.rb file:

config.include RSpec::Hanami::RequestHelpers

After that you can call any method:

it { expect(get('/')).to be_success }
it { expect(post('/tasks')).to redirect_to('/tasks') }

Controller Specs

have_http_status

Passes if response has a matching HTTP status code.

The following symbolic status codes are allowed:

  • :error
  • :missing
  • :redirect
  • :success
  • Rack::Utils::SYMBOL_TO_STATUS_CODE
response = action.call(params)
expect(response).to have_http_status(404)
expect(response).to have_http_status(:created)
expect(response).to have_http_status(:success)
expect(response).to have_http_status(:error)
expect(response).to have_http_status(:missing)
expect(response).to have_http_status(:redirect)

be_success

Passes if response has a not 4xx and 5xx error code.

response = action.call(params)
expect(response).to be_success

redirect_to

Passes if response has a redirect to special url

response = action.call(params)
expect(response).to redirect_to('site.com')

match_in_body

Passes if body matches with argument

response = action.call(params)
expect(response).to match_in_body('Tittle')
expect(response).to match_in_body(/Tittle\s\d+/)

include_json

Passes if json string in the body matches with hash arg

response = action.call(params)
expect(response).to include_json(name: 'Anton')
expect(response).to include_json(user: { name: 'Anton })

Views Specs

have_form_action

Passes if form object has an action

expect(view.form).to     have_form_action('/users')
expect(view.form).to_not have_form_action('/books')

have_method

Passes if form object has a method

expect(view.form).to     have_method('POST')
expect(view.form).to     have_method(:post)
expect(view.form).to_not have_method(:put)

have_form_field

Passes if form object has a field with wanted params

expect(view.form).to have_form_field(node: 'input', type: 'text', id: 'user-first-name')

Entity specs

Passes if argument type has a matching with type. You can use Hanami::Entity::Types for compare.

have_attribute

Passes if :name has Types::String type:

it { expect(User).to have_attribute(:name, Types::String) }

have_attributes

Passes if :name has Types::String type and :age has Types::Int type:

it { expect(User).to have_attributes(name: Types::String, age: Types::Int) }

Also see

Feature Requests & Bugs

See http://github.com/davydovanton/rspec-hanami/issues

License

The gem is available as open source under the terms of the MIT License.