Ruby on rails 如何对多态属性进行rspec测试验证?

Ruby on rails 如何对多态属性进行rspec测试验证?,ruby-on-rails,tdd,rspec-rails,polymorphic-associations,Ruby On Rails,Tdd,Rspec Rails,Polymorphic Associations,我正在尝试我在Rails和Rspec中的第一个TDD项目。我终于得到了一些用于模型验证的简单锅炉板Rspec代码。我的型号员工在电子邮件和地址中具有多态关联 如何为这些多态关联编写验证?除了确保验证:first\u name、:last\u name、:role的存在外,我还想确保存在电子邮件和地址 以下是我的模型: class Employee < ApplicationRecord has_many :employee_projects has_many :projec

我正在尝试我在Rails和Rspec中的第一个TDD项目。我终于得到了一些用于模型验证的简单锅炉板Rspec代码。我的型号
员工
电子邮件
地址
中具有多态关联

如何为这些多态关联编写验证?除了确保
验证:first\u name、:last\u name、:role的存在外,我还想确保存在电子邮件和地址

以下是我的模型:

class Employee < ApplicationRecord
    has_many :employee_projects
    has_many :projects, through: :employee_projects
    has_many :phones, as: :phonable, dependent: :destroy 
    has_many :emails, as: :emailable, dependent: :destroy 
    has_many :addresses, as: :addressable, dependent: :destroy

     accepts_nested_attributes_for :emails, allow_destroy: true

    validates_presence_of :first_name, :last_name, :role 

end

也希望开始使用FactoryBot,但希望先了解基本知识。

请查看。他们应该这么做。你的rails版本是什么。据我从Rails 5了解,
属于
是必需的,您在rspec中的
主题
应该无效。请检查。他们应该这么做。你的rails版本是什么。据我从Rails 5了解,
属于
是必需的,您在rspec中的
主题
应该无效。
class Address < ApplicationRecord
  belongs_to :addressable, polymorphic: true
end
class Email < ApplicationRecord
    belongs_to :emailable, polymorphic: true  
end
require 'rails_helper'

RSpec.describe Employee, type: :model do
  context 'Validation tests' do 
   subject { described_class.new(first_name: 'first_name', last_name: 'last_name', role: 'role') 
  }

    it 'is valid with attributes' do   
      expect(subject).to be_valid 
    end

    it 'is not valid without first_name' do   
      subject.first_name = nil 
      expect(subject).to_not be_valid
    end

    it 'is not valid without last_name' do
      subject.last_name = nil 
      expect(subject).to_not be_valid 
    end

    it 'is not valid without role' do    
      subject.role = nil 
      expect(subject).to_not be_valid 
    end


 end
end