Search⌘ K

Model Testing

Explore how to set up and run model tests in Ruby on Rails by using a dedicated test database and fixtures. Learn to write tests that verify model validations and ensure only valid data is saved. Understand how to identify and fix issues by running tests and applying model validations to improve your application's reliability.

Test database

Since your application relies heavily on interactions with a database, you need to have one in your testing environment. Rails provides a dedicated test database allowing you to run your tests in isolation, without having to worry about tampering with your development database.

Loading the database schema

To load the database schema for your test environment you will run:

rake db:schema:load RAILS_ENV=test

This loads the schema from your db/schema.rb file for your test database.

Note: This command will raise an error if there are any pending migrations.

Populating the database

To populate your database, you need to use fixturesfixtures. You will create the following entries in your test/fixtures/links.yml file (this file is auto-generated for your Link model when using scaffolding):

linkOne:
  url: mysite.com

linkTwo:
  title: myTitle
  url:
...