user3277633
user3277633

Reputation: 1923

NoMethodError: undefined method `validate_presence_of' with RSpec

Not sure what I am doing wrong here. This is my very first line of RSpec and I'm already running into errors.

Bill.rb

class Bill
  include Mongoid::Document
  field :name, type: String
  field :dollar_amount, type: Integer
  field :cent_amount, type: Integer

  validates :name, presence: true
  validates :dollar_amount, presence: true
  validates :cent_amount, presence: true
end

bill_spec.rb

require 'rails_helper'

RSpec.describe Bill, type: :model do
    it { is_expected.to validate_presence_of(:name) }
end

As far as I can tell, I did include validate_presence_of in my bill model. In fact, I am taking it exactly from the mongoid-Rspec doc What's going on here?

Upvotes: 0

Views: 505

Answers (1)

tadman
tadman

Reputation: 211740

You're using the old style of validation. Try this:

validates :name,
  presence: true

The validates method is much more flexible than the old specific ones and should be used in new applications. The documentation implies that both forms are supported, but the newer one should be encouraged.

Upvotes: 1

Related Questions