gpt4 book ai didi

ruby-on-rails - FactoryGirl 和 RSpec : create a record with required nested_attributes for specs

转载 作者:数据小太阳 更新时间:2023-10-29 09:00:43 24 4
gpt4 key购买 nike

我有一个模型 contact has_many :locations, through: :relationships,以及 has_many :teams, through: :contacts_teams .

联系人必须有关联的团队位置才能通过验证。换句话说:新的 contact 必须有关联的 relationship 记录和关联的 contacts_team 记录。以下是模型:

#models/contact.rb
class Contact < ActiveRecord::Base
has_many :contacts_teams
has_many :teams, through: :contacts

has_many :relationships, dependent: :destroy
has_many :locations, through: :relationships

accepts_nested_attributes_for :contacts_teams, allow_destroy: true

# upon create, validate that at least one associated team and one associated location exist
validate :at_least_one_contacts_team
validate :at_least_one_relationship

private

def at_least_one_contacts_team
return errors.add :base, "Must have at least one Team" unless contacts_teams.length > 0
end

def at_least_one_relationship
return errors.add :base, "Must have at least one Location" unless relationships.length > 0
end
end

#models/contacts_team.rb
class ContactsTeam < ActiveRecord::Base
belongs_to :contact
belongs_to :team
end

#models/team.rb
class Team < ActiveRecord::Base
has_many :contacts_teams
has_many :contacts, through: :contacts_teams
end

#models/relationship.rb
class Relationship < ActiveRecord::Base
belongs_to :contact
belongs_to :location
end

#models/location.rb
class Location < ActiveRecord::Base
has_many :relationships
has_many :contacts, through: :relationships
end

用于测试:使用 factory_girl我想创建一个能够成功创建 contact 记录的 contact 工厂。由于每个 contact 记录都需要关联的 contacts_team 记录和 relationship 记录:当我创建 contact 记录时应该创建那些也是。同样:contacts_team 记录应该有一个与之关联的现有 team,而 relation 记录应该有一个现有的 location 它关联到。所以本质上它应该创建一个 location 和一个 team 记录。

我如何创建与工厂的联系记录,这实际上创建了关联的 contacts_team 记录和 relationship 记录?

这是我目前的工厂:

FactoryGirl.define do
factory :contact do
first_name "Homer"
last_name "Simpson"
title "Nuclear Saftey Inspector"
end
end

FactoryGirl.define do
factory :contacts_team do
end
end

FactoryGirl.define do
factory :team do
name "Safety Inspection Team"
end
end

FactoryGirl.define do
factory :relationship do
end
end

FactoryGirl.define do
factory :location do
name "Safety Location"
end
end

如果很难/不可能用 factory_girl 做到这一点:我怎样才能用直接的 rspec 做到这一点?问题是我无法创建 contacts_team 记录或 relationship 记录,因为它关联的 contact 尚不存在!而且我无法创建 contact 记录,因为关联的 contacts_team 记录或 relationship 记录尚不存在。看起来我被困住了,但必须有一种不草率的方法来做到这一点。

最佳答案

我上周刚有一个类似的需求。

在你这个工厂的最后,你可以调用下一个工厂,然后他们就会关联起来。例如:

/spec/factories/contacts.rb

FactoryGirl.define do

factory :contact do |c|
first_name "Homer"
last_name "Simpson"
title "Nuclear Saftey Inspector"

# now, call the other two factories
relationship
contacts_team
end


factory :contacts_team do
# call the team factory
team
end


factory :relationship do
# call the location factory
location
end


# define the team and location factories...

end

现在,在/spec/controllers/contacts_controller_spec.rb 中

contact = FactoryGirl.create(:contact)

您可以只使用 factory girl 创建一个联系人,即使您只需要一个位置,因为所有内容都会立即生成。

备选方案(rspec)

不要“链接”你的工厂,而是在/spec/controllers/contacts_controller_spec.rb 中

contact = FactoryGirl.create(:contact)
# use .create_list(model, number, parent) to make children of a specific parent
contacts_team = FactoryGirl.create_list(:contacts_team, 3, :contact => contact)
relationship = FactoryGirl.create_list(:relationship, 3, :contact => contact)
team = FactoryGirl.create_list(:team, 3, :contacts_team => contacts_team)
location = FactoryGirl.create_list(:location, 3, :relationship => relationship)

这将创建一个联系人,有 3 个 contact_teams(有 3 个团队)和 3 个关系(有 3 个位置)

希望这可以帮助您找到制作测试数据的正确模式:)

关于ruby-on-rails - FactoryGirl 和 RSpec : create a record with required nested_attributes for specs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36207578/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com