Add this line to your application's Gemfile:
group :cli, :development, :test do
gem "hanami-minitest"
endAnd then execute:
$ bundle install
$ bundle exec hanami setup
For detailed examples, see EXAMPLES.md.
When you run hanami setup, this gem will automatically:
- Add test dependencies to your
Gemfile - Create a
test/test_helper.rbfile - Set up support files in
test/support/ - Generate an initial request test
The test support files generated by this gem use compact class notation to re-open existing Hanami::Minitest classes (e.g. class Hanami::Minitest::Test). If your RuboCop configuration enables Style/ClassAndModuleChildren, you'll need to exclude these files in your root .rubocop.yml:
Style/ClassAndModuleChildren:
Exclude:
- "test/support/**/*"Generate an action with its test:
$ bundle exec hanami generate action home.index --url=/
This creates both the action and a corresponding test file.
Request tests use Rack::Test to test your application's HTTP interface:
require "test_helper"
class RootTest < RequestTest
def test_successful_response
get "/"
assert_equal 200, last_response.status
end
endFeature tests use Capybara for browser-based integration testing:
require "test_helper"
class SignUpTest < FeatureTest
def test_visit_home_page
visit "/"
assert_text "Welcome to Hanami"
end
endWhen generating a view part, a test is automatically created:
require "test_helper"
class MyApp::Views::Parts::UserTest < Minitest::Test
def setup
@value = Object.new
@subject = MyApp::Views::Parts::User.new(value: @value)
end
def test_works
assert_kind_of MyApp::Views::Parts::User, @subject
end
endIf you're using hanami-db, database cleaning is automatically configured using database_cleaner-sequel.
Feature tests automatically get database cleaning. For other tests that need database access, simply include the TestSupport::DB module:
require "test_helper"
class MyDatabaseTest < Minitest::Test
include TestSupport::DB # This test will get database cleaning
def test_database_operation
# Database is cleaned before and after this test
end
endBy default, tests use database transactions (fast). If you need truncation (e.g., for JavaScript-driven tests), define a use_truncation? method:
class DynamicFormTest < FeatureTest
def use_truncation?
true # Use truncation instead of transactions
end
def test_with_javascript
# Uses truncation strategy
end
endThe gem includes support for testing operations that use dry-monads:
require "test_helper"
class MyOperationTest < Minitest::Test
def test_successful_operation
result = MyOperation.new.call(valid_params)
assert result.success?
end
def test_failed_operation
result = MyOperation.new.call(invalid_params)
assert result.failure?
end
endAfter checking out the repo, run bin/setup to install dependencies. Then, run rake test to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and the created tag, and push the .gem file to rubygems.org.
Bug reports and pull requests are welcome on GitHub at https://github.com/hanami/hanami-minitest. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.
See LICENSE file.