How to Test Rails Models with Minitest

27-Aug-2016 5846

In this tutorial, we will cover how to test Ruby on Rails models with the Minitest testing suite. After completing this tutorial, you will have learned the following:

  • What to test in Rails models,
  • How to test the various aspects of a Rails model, such as validations, associations, scopes, and extra business logic methods, and
  • How to use fixtures to refactor and simplify your model tests.

We will be using Ruby 2.3, Rails 5.0 and Minitest 5.8. However, older versions should have the same behavior. The only difference to note is that models in Rails 5.0 inherit fromApplicationRecord instead of ActiveRecord::Base.

Free eBook download

Download all our eBooks for free from Gumroad!
Prepare for your next tech interview with our comprehensive collection of programming interview guides. Covering JavaScript, Ruby on Rails, React, and Python, these books offer thousands of essential questions and answers across multiple languages. Download the complete collection for free from our Gumroad page. Free Ultimate Job Interview Preparation eBook collection