简体   繁体   English

Rails:RSpec无法通过模型子类的验证

[英]Rails: RSpec fails validations of model subclass

I have a Rails 5 setup where RSpec fails to check validations on model subclass. 我有一个Rails 5设置,其中RSpec无法检查模型子类的验证。 If I manually build the object in console I am able to see the errors which should prevent the record to be valid. 如果我在控制台中手动构建对象,我将看到错误,这些错误将导致记录无效。

The base model: 基本模型:

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

The model which inherits from Article: 从Article继承的模型:

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

The factory for these models (FactoryGirl): 这些模型的工厂(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

This is the RSpec which doesn't work: 这是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

The first spec pass, the other two don't. 第一个规格通过,其他两个不通过。 I tried to test everything in the console, with the same values, and it works, meaning that the record is invalid as it should be. 我试图用相同的值测试控制台中的所有内容,并且它可以正常工作,这意味着该记录应该是无效的。

Is it possible that with RSpec the validations in the subclass won't work? 使用RSpec是否有可能无法在子类中进行验证?

I'm sorry for the delay, but I think I have figured out what was breaking my tests. 很抱歉造成您的延误,但我想我已经弄清了什么原因破坏了我的考试。

The problems, actually, were two, and not one as I originally thought. 实际上,问题是两个,而不是我最初想到的一个。

The first test: should have an excerpt 第一次测试: should have an excerpt

As suggested by juanitofatas, I have added a byebug line after the one where FactoryGirl builds my model. 根据juanitofatas的建议,我在FactoryGirl建立我的模型的byebug行之后添加了一条byebug行。 I have noticed that the model instantiated had class Article and not LongArticle . 我注意到实例化的模型具有class Article而不是LongArticle

I came up noticing that FactoryGirl instantiate a model of the base factory when it first met factory :article do . 我突然注意到FactoryGirl在首次遇到factory :article do时实例化了基础工厂的模型。 Then, it adds or overrides the attributes defined into inner factories and treating the type attribute as any one other, ignoring that it drives the STI. 然后,它将添加或覆盖定义到内部工厂中的属性,并将type属性视为任何其他属性,而无需考虑它会驱动STI。

The LongArticle factory should have been defined as a completely different model, at the same level as the Article one is. LongArticle工厂应该被定义为完全不同的模型,与Article相同。

The second test: should have the thumbnail 第二项测试: should have the thumbnail

This was a bit silly... I have defined a default_url method in the CarrierWave uploader and, in fact, this is the desired behavior. 这有点愚蠢……我在CarrierWave上传器中定义了default_url方法,实际上,这是所需的行为。 Test was updated. 测试已更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM