RSpec 3: Simple validates_uniqueness_of Failure

I am trying to test the uniqueness check on a model that will fail. I have an identical validation test on a different model that passes, but I don't understand what I am missing on this.

I searched around the forum and tried several things but still didn't get through. I'm sure there is something small and important here!

Here is my code ..

factories.rb

FactoryGirl.define do

    factory :quote do
        sequence (:content) { |x| "Dust is dirty word number #{x}!" }
        reference "http://en.wikipedia.org/wiki/Albert_Einstein"
        author_id 1
    end

end

      

quote_spec.rb

require 'rails_helper'
require 'shoulda/matchers'

RSpec.describe Quote, :type => :model do
    quote = FactoryGirl.build(:quote)

  it { expect(quote).to belong_to(:author) }

    describe "attributes" do

        it "saves attributes" do
            quote.save!
            expect(quote).to be_valid
        end

        it "stores author id" do
            expect(quote[:author_id]).to eq(1)
        end
  end


    context "validations" do
        it { expect(quote).to validate_presence_of   :content }
        it { expect(quote).to validate_uniqueness_of :content, message: "That been said before!" }
    end

end

      

quote.rb

class Quote < ActiveRecord::Base
    belongs_to :author, inverse_of: :quote
    validates :content, :reference, presence: :true
    validates_uniqueness_of :content, message: "That been said before!"
end

      

malfunction

Quote
  should belong to author
  attributes
    saves attributes
    stores author id
  validations
    should require content to be set
    example at ./spec/models/quote_spec.rb:24 (FAILED - 1)

Failures:

  1) Quote validations 
     Failure/Error: it { expect(quote).to validate_uniqueness_of :content, message: "That been said before!" }
     ArgumentError:
       wrong number of arguments (2 for 1)
     # ./spec/models/quote_spec.rb:24:in `block (3 levels) in <top (required)>'

      

+3


source to share


1 answer


That's right, just a small syntax problem to fix:



it { is_expected.to validate_uniqueness_of(:content).with_message("That been said before!") }

      

+4


source







All Articles