After using RSpec for several projects, I'm giving minitest/unit a go. I'm liking it so far, but I miss using describe/context blocks to group my tests/specs in a logical way.
I know minitest/spec provides this functionality, but I like that minitest/unit feels a bit closer to barebones Ruby.
Are there any gems that provide describe/context support for minitest/unit? Or, should I just live with my long, unorganized test files in minitest/unit?
Setting Up Minitest. To run a Minitest test, the only setup you really need is to require the autorun file at the beginning of a test file: require 'minitest/autorun' . This is good if you'd like to keep the code small. A better way to get started with Minitest is to have Bundler create a template project for you.
We can run all of our tests at once by using the bin/rails test command. Or we can run a single test file by passing the bin/rails test command the filename containing the test cases. This will run all test methods from the test case.
If your tests don't require any specific actions before start and you don't want to configure additional options, such as code coverage, you can run them by using the following options: Place the caret at the test class to run all tests in that class, or at the test method, and press Ctrl+Shift+F10 .
They make sure that a section of an application, or a “unit”, is behaving as intended. In a Rails context, unit tests are what you use to test your models. Although it is possible in Rails to run all tests simultaneously, each unit test case should be tested independently to isolate issues that may arise.
I know several folks coming from RSpec to minitest struggling with the same question. They love the ability to nest using describe/context blocks and want to continue in minitest. There are several solutions:
context
blocks. But you can just as easily use describe
in its place and everything works as you'd expect.Here is an example of how my test files are organized:
test/ models/ user/ authentication_test.rb email_test.rb reservation_test.rb user_test.rb username_test.rb
I use this structure whether I'm using the spec DSL or the xUnit style. When using the spec DSL I specify what I'm testing in my describe block like so:
require "minitest_helper" describe User, :authentications do before do # ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With