Search⌘ K
AI Features

Unit Testing

Explore unit testing in Rails 6 by understanding how to validate models and write test assertions using the MiniTest framework integrated into Rails. Learn to check data integrity with practical examples and run tests efficiently from the Rails command line.

One of the joys of the Rails framework is that it has support for testing baked right in from the start of every project. From the moment we create a new application using the rails command, Rails starts generating a test infrastructure for us. Inside the model subdirectory lies this file:

depot> ls test/models 
product_test.rb

The product_test.rb file is what Rails created to hold the unit tests for the model we created earlier with the generate script. This is a good start, but Rails can only help us so much. Let’s see what kind of test goodies Rails generated inside test/models/product_test.rb ...