Minitest Setup

Learn more on Minitest setup, setup blocks and Minitest factories.

One of the clearest differences between the RSpec and Minitest ways is setting up different data for different tests. In RSpec, the describe method is used to enclose an arbitrary number of tests to give each group of tests a different setup. For example, our Project specs in spec/models/project_spec.rb file have three different groups, one without a task, one with a task, and one with a bunch of tasks.

Minitest setup

In Minitest, each class has one setup method.

Note: Technically, we can define multiple setup do blocks, but all of them are executed for all tests. We can’t use Minitest setup to have different setups for different tests.

While we could solve this problem by putting each setup in a different class, we think it’s somewhat more common to have explicit methods called from the beginning of the test to handle setup.

Minitest multiple setup blocks

Here’s one way to handle multiple setup blocks in a Minitest version of our Project tests (we are running the test using bundle exec rake test):

Get hands-on with 1200+ tech skills courses.