Coder Social home page Coder Social logo

rspec-hanami's Introduction

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.

rspec-hanami's People

Contributors

adodulad avatar davydovanton avatar egorbazhenov avatar en1ck avatar fedcomp avatar ikzekly avatar sadovnik avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

rspec-hanami's Issues

Form matcher's failure message unexpected behaviour

Ok, I would actually address 3 issues here but since they all related to the same method they might have a single solution.

First

Each matcher includes following code:
failure_message { |actual| "expect #{form.inspect} (#{attributes}) to have #{method} method param" }
Anytime any of the matchers fails I receive the following error:

Failure/Error: it { expect(view.form).to have_form_field(node: "input", type: "password") }
   FrozenError: can't modify frozen String

This error comes from the #{form.inspect} operation when RSpec generates failure message and originates in haml's inspect method (which calls gsub! with a frozen string option enabled).

I was not able to reproduce this error using rspec-hanami repo and haml though. However I'm sure I'm just missing out something that happens under Hanami's hood.

Second

Whenever form matcher fail failure_message would output multi page diff of a FormBuilder object which is overkill.

Third

have_field matcher's failure_message includes errors. Look at that, there is no #{form.inspect}, #{attributes} or #{method} defined within matcher. Obviously that's must be a copy-paste issue from 'have_method' matcher.

matcher :have_field do |params|
  require 'hanami/utils/hash'
  attr_reader :form, :form_data, :params

  description { "have field with params" }
  match do |form|
    @form = form
    @params = ::Hanami::Utils::Hash.new(params).symbolize!
    @form_data = RSpec::Hanami::FormParser.new.call(form.to_s)

    form_data.any? do |input|
      input.merge(params) == params.merge(input)
    end
  end

  failure_message { |actual| "expect #{form.inspect} (#{attributes}) to have #{method} method param" }
  diffable
end

Solution

So as I mentioned one approach might handle all three issues:

  1. Output @form_data = RSpec::Hanami::FormParser.new.call(form.to_s) instead of '#{form.inspect}' in `failure_message' of every matcher.
  2. Remove diffable for every form matcher.
  3. Fix have_field matcher's failure_message with params.

Failing when preloading

~Code/Experiment/oneman/spec/support/capybara.rb:5:in included': uninitialized constant RSpec::Hanami::Container (NameError)`

Hanami head version.

Request tests

Any plan to support request tests with Hanami? (sequences of black-box request-response cycles going through the whole Hanami app including middlewares)

RSpec::Hanami::RequestHelpers doesn't exist?

After including RequestHelpers ala config.include RSpec::Hanami::RequestHelpers I get noMethodError: undefined method post'`

When reviewing the source code I can't see RSpec::Hanami::RequestHelpers anywhere. Am I missing something here?

Have_filed matcher conflicts with Capybara

When using have_field matcher in unit tests and running and unit and integration tests altogether have_field matcher from Capybara's matchers would be used since feature_spec loads Capybara's matchers after RSpec::Hanami matchers has been loaded in spec_helper.

Proposal: rename have_field matcher to have_input.

Test entity schema

I think it will be cool to test entity schema like this:

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.