Alphy Gacheru
Alphy Gacheru

Reputation: 657

Why my example is passing test without model validation?

I've got the following model and test file. To my understanding, the last example should fail until I validate the body attribute in the model but it's passing the test. I'm not sure what it is that I'm missing. Any assistance is highly appreciated in advance, thanks.

article.rb

class Article < ApplicationRecord
  validates :title, presence: true, length: { in: 6..25 }
end

article_spec.rb

require 'rails_helper'

RSpec.describe Article, type: :model do
  subject { Article.new(title: 'Lorem ipsum dolor sit, amet ', body: 'consectetur adipisicing elit. Unde, labore?') }
  before { subject.save }

  it 'is not valid without a title' do
    subject.title = nil
    expect(subject).to_not be_valid
  end

  it 'is not valid if the title is too short' do
    subject.title = 'a'
    expect(subject).to_not be_valid
  end

  it 'is not valid if the title is too long' do
    subject.title = 'a' * 26
    expect(subject).to_not be_valid
  end

  it 'is not valid without a body' do
    subject.body = nil
    expect(subject).to_not be_valid
  end
end

Upvotes: 0

Views: 58

Answers (1)

Prem Anand
Prem Anand

Reputation: 1496

You are missing the validation for the body attribute.

class Article < ApplicationRecord
  validates :title, presence: true, length: { in: 6..25 }
  validates :body, presence: true
end

Upvotes: 1

Related Questions