RSpecがモデルサブクラスの検証をチェックできないRails 5セットアップがあります。コンソールにオブジェクトを手動で構築すると、レコードが有効でないようなエラーが表示されます。Rails:RSpecがモデルサブクラスの検証に失敗する
ベースモデル:
class Article < ApplicationRecord
belongs_to :author, class_name: User
validates :author, presence: { message: "L'utente autore dell'articolo è obbligatorio." }
validates :title, presence: { message: "Il titolo dell'articolo è obbligatorio." }
end
条から継承モデル:
class LongArticle < Article
mount_uploader :thumbnail, LongArticleThumbnailUploader
validates :excerpt, presence: { message: "L'estratto dell'articolo è obbligatorio." }
validates :thumbnail, presence: { message: "L'immagine di anteprima dell'articolo è obbligatoria." }
end
工場これらのモデルの(FactoryGirl):
FactoryGirl.define do
factory :article do
association :author, factory: :author
title "Giacomo Puccini: Tosca"
factory :long_article do
type "LongArticle"
excerpt "<p>Teatro alla Scala: immenso Franco Corelli.</p>"
thumbnail { Rack::Test::UploadedFile.new(File.join(Rails.root, 'spec', 'support', 'images', 'unresized-long-article-thumbnail.jpg')) }
end
end
end
これは動作しませんRSpecのです:
require 'rails_helper'
RSpec.describe LongArticle, type: :model do
describe "is valid with mandatory fields" do
it "should be valid with if all mandatory fields are filled" do
article = FactoryGirl.create(:long_article)
expect(article).to be_valid
end
it "should have an excerpt" do
article = FactoryGirl.create(:long_article)
article.excerpt = nil
expect(article).not_to be_valid
end
it "should have the thumbnail" do
article = FactoryGirl.create(:long_article)
article.thumbnail = nil
expect(article).not_to be_valid
end
end
end
最初のスペックパス、他の2つはありません。 同じ値でコンソールのすべてをテストしようとしましたが、それは動作します。つまり、レコードが無効であるはずです。
RSpecでサブクラスの検証が機能しない可能性はありますか?
あなたが理解している間、簡単に検証をテストするためにhttps://github.com/thoughtbot/shoulda-matchersをチェックすることをお勧めします。 – juanitofatas
また、デバッガやbiding.pryを使って仕様に 'article.valid?'を手動で実行して、その理由を調べることもできます。 – juanitofatas