gpt4 book ai didi

ruby-on-rails - ActiveRecord Relation where method matching on unique id returns object slightly diff then when traversing the array and matching on the same id

转载 作者:太空宇宙 更新时间:2023-11-03 16:29:14 25 4
gpt4 key购买 nike

对 ruby​​、rails 和单元测试还很陌生,欢迎任何其他反馈!

我正在为名为#assign_campaign 的基于 ActiveRecord 的模型(联系人)编写 rspec 单元测试。 ContactCampaign 是一个将 Contacts 映射到 Campaigns 的关联。这里的想法是,当一个事件被分配时,该联系人的所有其他事件都将被停用,而被分配的事件将被激活。

测试失败,因为它声称分配的事件的状态为零且不是“事件”(或“非事件”)。

似乎归结为 campaign_id 属性上的“where”匹配似乎没有返回同一个对象,就好像我循环访问 contact_campaigns 并匹配 campaign_id 一样。

例如

contact.contact_campaigns.where(campaign_id: campaign_to_be_assigned.id).first.inspect

让我:

object_id 为:70199917191760

同时

contact.contact_campaigns.each do |x|
if x.campaign_id == campaign_to_be_assigned.id
puts x.inspect
end
end

让我:

object_id 为:70199940110940

这是怎么回事?这是 ActiveRecord 失败、FactoryGirl 失败还是简单的 Ruby 失败? ;p

如果我从以下位置重写 assign_campaign:

current_contact_campaign = contact_campaigns.where(campaign_id: campaign_id).first
current_contact_campaign.activate
current_contact_campaign.set_current_position(starting_position) unless
starting_position == 0

收件人:

contact_campaigns.each do |x|
if x.campaign_id == campaign_id
x.activate
x.set_current_position(starting_position) unless
starting_position == 0
end
end

然后一切都很开心,但我想知道是怎么回事。

Rspec 测试:

it "assign_campaign assigns a campaign, creates a new campaign task,
note and deactivates other contact campaigns, one was active previously" do
# not checking the note portion right now

contact = FactoryGirl.create(:contact, user: user)
campaign_to_be_assigned = FactoryGirl.create(:campaign)
campaign_to_be_assigned.messages << FactoryGirl.create(:message)

5.times do |i|
contact.campaigns << FactoryGirl.create(:campaign)
end
contact.deactivate_contact_campaigns

# Randomly activates one of the generated campaigns
contact.contact_campaigns[rand(4)].activate

contact.assign_campaign(campaign_to_be_assigned.id)

contact.contact_campaigns.each do |x|
if x.campaign_id == campaign_to_be_assigned.id
x.status.should == 'active'
else
x.status.should == 'inactive'
end
end
end

正在测试的方法:

def assign_campaign(campaign_id=nil, starting_position = 0,  opts = {})
if email_is_valid || campaign_id == nil
if !campaign_id.blank?
campaign = Campaign.find(campaign_id)
deactivate_contact_campaigns
campaigns << campaign if !campaigns.include? campaign
current_contact_campaign = contact_campaigns.where(campaign_id: campaign_id).first
current_contact_campaign.activate
current_contact_campaign.set_current_position(starting_position) unless
starting_position == 0
notes.create(user_id: user.id, body: "Assigned #{name} to " +
campaign.description, group: "system",
created_at: (opts[:assign_time] ? opts[:assign_time] : Time.now))
create_new_campaign_task(opts[:user_id],
(starting_position == 0 ? 0 : (starting_position - 1)))
return true
else
notes.create(user_id: user.id,
body: "Unassigned #{name} from campaign", group: "system",
created_at: (opts[:assign_time] ? opts[:assign_time] : Time.now)) if
contact_campaigns.where(status: 'active').size > 0
deactivate_contact_campaigns
end
else
return false
end
end

测试失败消息

Failures:

1) Contact assign_campaign assigns a campaign, creates a new campaign task,
note and deactivates other contact campaigns, one was active previously
Failure/Error: x.status.should == 'active'
expected: "active"
got: nil (using ==)
# ./spec/models/contact_spec.rb:78:in `block (3 levels) in <top (required)>'
# ./spec/models/contact_spec.rb:76:in `block (2 levels) in <top (required)>'

FactoryGirl 工厂

事件

FactoryGirl.define do
factory :campaign do |campaign|
sequence(:description) { |n| "description#{n}"}
end
end

联系人

require 'faker'

FactoryGirl.define do
factory :contact do |contact|
fake_email = Faker::Internet.email
association :organization, factory: :organization
sequence(:email) { |n| "#{n}#{fake_email}" }
end
end

消息

FactoryGirl.define do
factory :message do |message|
message.description { "Considering buying your first home?" }
message.position { 0 }
message.interval { 0 }
message.email_subject { "Considering buying your first home?" }
message.email_body { "I have several valuable resources if you are considering buying your first home. I'm happy to discuss the process of buying a home in simple, easy-to-understand terms, or I can help point you in the right direction to get the best loan for a home purchase.\n\nIf you're just curious what you can get for your money, I'm happy to show you a few properties in your area so you can see what is available. Give me a call today anytime. I look forward to hearing from you." }
end
end

最佳答案

关于您关于 object_id 的第一个问题:

object_id 是 Ruby 中分配给实际实例的内部引用 ID。任何两个实例都不能具有相同的 object_id。这与在 Rails 中调用 .id 不同,后者应返回数据存储中记录的 ID。

您的测试可能失败的原因:你打电话的那一刻:

contact.contact_campaigns[rand(4)].activate

Rails 访问您的数据存储并在本地缓存集合。然后你“激活”了项目 rand(4),这显然是你的意图。

然后你这样调用:

contact.assign_campaign(campaign_to_be_assigned.id)

它应该停用之前激活的 rand(4)。但是,在 contact.contact_campaigns 的内存中的数据存储中NOT。所以请记住 contact.contact_campaigns 仍在内存中并认为 rand(4) 已激活。所以在你这样做之前:

contact.contact_campaigns.each do...

您应该重新加载集合:

contact.contact_campaigns.reload

希望这对您有所帮助!

关于ruby-on-rails - ActiveRecord Relation where method matching on unique id returns object slightly diff then when traversing the array and matching on the same id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18753430/

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