How to Test Rails Models with RSpec

Testing is where we spend most of our time as developers. Good testing raises the quality of software, reduces bugs and, in the long run, makes our work easier.

In this article, we’ll discuss the basics of testing with Ruby on Rails:

  • What is BDD?
  • How to test models in Rails?
  • How to test business logic with Rspec?
  • How to use Continuous Integration to automate testing?

Hey :slight_smile:

Thank you for this easy to read and expert article!
i think there is a little mistake on testing models validation :

subject {
    described_class.new(title: "Anything",
                        description: "Lorem ipsum",
                        start_date: DateTime.now,
                        end_date: DateTime.now + 1.week,
                        user_id: 1)
  }

At this point, Auction doesn’t belongs to a user. So the test can’t pass.

On Business Behaviour :

        require 'rails_helper'

    RSpec.describe Auction, :type => :model do

      let(:seller) {
        User.new(:email => "jane@doe.com", :password => "pw1234")
      }
      subject {
        described_class.new(title: "Anything",
                            description: "Lorem ipsum",
                            start_date: DateTime.now,
                            end_date: DateTime.now + 1.week,
                            user_id: 1)
      } 

This is working ? The user is not created, just instantiated right ?