Minitest: model & assertions
This course covers Minitest, the default testing framework built into Rails, in an app whose own tests are written in RSpec. It reads ActiveSupport::TestCase and its parent class Minitest::Test from the installed gem source. It runs assert_equal, assert_predicate and assert_difference against a disposable ActiveRecord model on Postgres and shows their failure messages. It explains why ActiveRecord::TestFixtures is installed but never wired in. It also translates two RSpec examples into Minitest and closes with a small challenge in pure Ruby.
What you'll learn
- ✓How ActiveSupport::TestCase subclasses Minitest::Test and inherits Minitest::Assertions
- ✓How the declarative test helper turns a string into a test_ method with define_method
- ✓Using assert_equal and assert_predicate on ActiveRecord models and reading their failure output
- ✓How assert_difference and its related assertions check what a block changes
- ✓Why ActiveRecord::TestFixtures depends on rails/test_help.rb and a test/test_helper.rb
- ✓How RSpec expectations map to Minitest assertions, and where they do not
Minitest exists here: this app's tests are RSpec, and that's the starting point
This app's tests live in spec/ and run with bundle exec rspec spec/. Gemfile line 60 says gem "rspec-rails". There is no test/ directory, which is where a Minitest suite would live. If a course ever tells you this app "has no tests", that course checked the wrong convention.
Read the full lesson free →Curriculum
7 lessonsMinitest exists here: this app's tests are RSpec, and that's the starting point
Shows that this app's tests are RSpec in spec/ while Minitest ships installed as Rails' default framework.
ActiveSupport::TestCase: the class every Minitest model test builds on
Reads how ActiveSupport::TestCase subclasses Minitest::Test and how the declarative test helper defines ordinary test methods.
assert_equal and assert_predicate, run against an ActiveRecord model
Runs assert_equal and assert_predicate against a disposable ActiveRecord model and shows the Expected/Actual diff on failure.
assert_difference: Rails' addition, testing what a block changes
Covers assert_difference, an ActiveSupport addition to Minitest, including its before and after evaluation and failure message.
ActiveRecord::TestFixtures: installed, and never wired into this app
Explains test file naming and why ActiveRecord::TestFixtures stays unconnected without a test/test_helper.rb requiring rails/test_help.
Side by side: this app's RSpec, translated to Minitest
Translates two RSpec examples from the account update spec into Minitest and maps each assertion side by side.
Putting it together: a valid framework this app didn't choose
Reviews the mental model and sources of the course, then poses a pure Ruby challenge building mini_assert_difference.