Model Tests

Let’s look at unit tests for models. These are the most common unit
tests you’ll see in Rails applications.

Before writing a unit test, we’ll create a User model in our tutorial
application. We’ll use a Rails generator:

$ rails generate model user email:string

This creates a folder for model specs:

    spec/models

As well as several files:

    db/migrate/…_create_users.rb
    app/models/user.rb
    spec/models/user_spec.rb
    spec/factories/users.rb

Run the migration to set up the database for the development
environment:

$ rake db:migrate

RSpec is configured to clone migrations automatically (see the “Run
RSpec” section of this guide). If not, you’ll have to run rake
db:test:clone.