我正在尝试在无法通过的模型上测试唯一性验证。我对另一个通过的模型进行了相同的验证测试,但我不明白我在这方面遗漏了什么。
我已经在论坛上搜索并尝试了一些东西,但仍然没有通过。我敢肯定这里有一些小而重要的东西要学!
这是我的代码..
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/