5

In my model I have a before_validation method :

class Post < ActiveRecord::Base
 before_validation :generate_url
 ...

 private

def generate_url
   return unless self.url.blank?
   year = self.published_at.class == ActiveSupport::TimeWithZone ? self.published_at.year : DateTime.now.year
   self.url = "#{year}/#{self.title.parameterize}"
end

rspec/models/post.spec.rb

testing w rspec-rails (3.1.0) and factory_girl_rails(4.4.1), I got a spec/models/post_spec.rb to validate the Post model and a spec/factories/post.rb

validation tests are failing because of the before_validation

 Post should require title to be set
 Failure/Error: it { is_expected.to validate_presence_of(:title) }
 NoMethodError:
   undefined method `parameterize' for nil:NilClass
 # ./app/models/post.rb:69:in `generate_url'
 # ./spec/models/post_spec.rb:20:in `block (2 levels) in <top (required)>'

with a debug statement in the generate_url method, I get self as nil ... so it seems the factory post is not taken in account

spec/models/post_spec.rb

require 'rails_helper'

RSpec.describe Post, :type => :model do
  it { is_expected.to validate_presence_of(:title) }
  it { is_expected.to ensure_length_of(:title).is_at_least(5) }
  it { is_expected.to validate_presence_of(:content) }
  it { is_expected.to ensure_length_of(:content).is_at_least(10) }
  it { is_expected.to validate_presence_of(:url) }
  it { is_expected.to validate_uniqueness_of(:url) }
  it { is_expected.to validate_presence_of(:published_at) }
end

spec/factories/post.rb

FactoryGirl.define do
  factory :post do
    user_id 1
    title "myTitle"
    content "myContent."
    published false
    published_at "2014-09-16 08:46:38"
  end
end
# note : attribute url is generated with before_validation
2
  • post generate_url mehtod code and complete test code for detailed answer. You have to either stub generate_url or provide some default data to the post, making generate_url work properly
    – dre-hh
    Sep 22, 2014 at 11:07
  • data are provided to the post with the factories/post.rb, unless I missed any additional setup to correctly read it during spec processing ?
    – user762579
    Sep 22, 2014 at 12:58

2 Answers 2

6

Got it to skip the before_validation:

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

  before{ Post.skip_callback(:validation, :before, :generate_url) }

  it { is_expected.to validate_presence_of(:user_id) }
  it { is_expected.to validate_presence_of(:title) }
  it { is_expected.to ensure_length_of(:title).is_at_least(5) }
  it { is_expected.to validate_presence_of(:content) }
  it { is_expected.to ensure_length_of(:content).is_at_least(10) }
  it { is_expected.to validate_presence_of(:url) }
  it { is_expected.to validate_uniqueness_of(:url) }
  it { is_expected.to validate_presence_of(:published_at) }
end

You can also provide a block so the validation is only skipped for the block, like so:

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

  Post.skip_callback(:validation, :before, :generate_url) do 
    it { is_expected.to validate_presence_of(:user_id) }
    it { is_expected.to validate_presence_of(:title) }
    it { is_expected.to ensure_length_of(:title).is_at_least(5) }
    it { is_expected.to validate_presence_of(:content) }
    it { is_expected.to ensure_length_of(:content).is_at_least(10) }
    it { is_expected.to validate_presence_of(:url) }
    it { is_expected.to validate_uniqueness_of(:url) }
    it { is_expected.to validate_presence_of(:published_at) }
  end
end
0

NOTE: self is not nil. self.title is nil

According to the shoulda doc the spec could be fixed by setting a proper subject like this

RSpec.describe Post, :type => :model do
  subject { FactoryGirl.create :post }
  it { is_expected.to validate_presence_of(:title) }
end

However i tried this out and the error still occur. validate_presence_of just ignores the subject and resets it to Post.new. It allways ignores the expected thing. I would say it's a shoulda bug. Wiil check their code later or at least submit the issue.

In the meantime just do it without shoulda the ol'school way

RSpec.describe Post, :type => :model do
    describe #valiadations do
      let!(:post) {Post.new.tap{|p|p.valid?} }
      it "validated title" do
        expect(post.errors_on[:title.size]).to eq(1) 
      end 
    end
 end
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.