RSpec 3: Simple validates_uniqueness_of Failure
factory-bot, rspec, ruby, ruby-on-rails, validation
Solution
Right, just a small syntax issue to fix:
it { is_expected.to validate_uniqueness_of(:content).with_message("That's been said before!") }
Problem
I am trying to test the uniqueness validation on a model that won't pass. I have an identical validation test on another model that passes but I don't understand what I am missing on this. I have searched around the forum and tried a few things but still no pass. I'm sure there is something small and significant to learn here! Here's 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's 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's been said before!" end ``` failure ``` 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's been said before!" } ArgumentError: wrong number of arguments (2 for 1) # ./spec/models/quote_spec.rb:24:in `block (3 levels) in <top (required)>' ```