gpt4 book ai didi

ruby-on-rails - 在 Rails 测试中给定一个 id,如何找到 fixture 的名称?

转载 作者:行者123 更新时间:2023-11-28 20:30:05 25 4
gpt4 key购买 nike

Rails 提供了一种在给定 fixture 名称的情况下查找 ID 的方法:

参见 [ActiveRecord::FixtureSet.identify]( http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html#method-c-identify

假设,在一个失败的测试中,我想根据 ID 打印出有问题的 fixture 的名称。如何进行反向查找?

最佳答案

这是我想出的一些代码,直到出现更好的代码。它遍历给定表的所有已加载固定装置,如果“标识”给定 ID,则返回名称。

class ActiveRecord::FixtureSet
def self.reverse_lookup(table, id)
ActiveRecord::FixtureSet.all_loaded_fixtures[table.to_s].each do |key, _|
return key if ActiveRecord::FixtureSet.identify(key) == id
end
nil
end
end

关于ruby-on-rails - 在 Rails 测试中给定一个 id,如何找到 fixture 的名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32322280/

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