ruby-on-rails - RSpec 3 : Simple validates_uniqueness_of Failure

标签 ruby-on-rails ruby validation rspec factory-bot

我正在尝试在无法通过的模型上测试唯一性验证。我对另一个通过的模型进行了相同的验证测试,但我不明白我在这方面遗漏了什么。

我已经在论坛上搜索并尝试了一些东西,但仍然没有通过。我敢肯定这里有一些小而重要的东西要学!

这是我的代码..

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

引用.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

失败

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)>'

最佳答案

好的,只是一个小的语法问题需要解决:

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

关于ruby-on-rails - RSpec 3 : Simple validates_uniqueness_of Failure,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27217906/

相关文章:

javascript - 用户突出显示输入字段并且未输入任何内容后,Angular JS 会显示错误

ruby-on-rails - 为什么我得到 NoMethodError?

html - Ruby on Rails 链接、导航

ruby-on-rails - 使用 Kernel#fork 进行后台进程,专业人士?缺点?

ruby-on-rails - rails 4 : Model Logic - Counting Items by a User

forms - Dojo dijit.form.Select验证错误显示

ruby-on-rails - 启动Rails服务器时出错:未定义的方法 'configure'

ruby - 很多 H17 Heroku 错误

javascript - 无法在 Rails 5.0.0 中限制文件上传页面的重定向?

javascript - 表单验证只能使用 type=button 而不能使用 type=submit?