Rspec失败期望错误_on。预计1,得到8

时间:2013-11-19 03:52:08

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

所以我有一个工厂

factory :comment do
    sequence :text do |n|
        "I like this#{n}"
    end
    association :post
end

我对rspec的期望表明了这一点。

  it "does not allow duplicate comments on one post" do
    post = build(:post)
    comment1 = create(:comment, post: post)
    comment2 = build(:comment, post: post)
    expect(:comment2).to have(1).errors_on(:text)
end

然而它一直没说:

1) Comment does not allow duplicate comments on one post
 Failure/Error: expect(:comment2).to have(1).errors_on(:text)
   expected 1 errors on :text, got 8
 # ./spec/models/comment_spec.rb:8:in `block (2 levels) in <top (required)>'

我似乎无法弄清楚为什么它有8个错误以及它们会是什么。

1 个答案:

答案 0 :(得分:0)

您的规范中有拼写错误 - comment2是变量,而不是符号,因此它应该是expect(comment2).to have(1).errors_on(:text)