【问题标题】:How do I find a fixture's name, given an id in Rails tests?给定 Rails 测试中的 id,我如何找到夹具的名称?
【发布时间】:2015-09-01 01:37:01
【问题描述】:

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

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

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

【问题讨论】:

    标签: ruby-on-rails testing fixtures


    【解决方案1】:

    这是我想出的一些代码,直到出现更好的代码。它遍历给定表的所有加载的固定装置,如果“识别”到给定的 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-24
      • 1970-01-01
      • 2016-11-02
      • 2020-08-21
      • 1970-01-01
      • 2018-03-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多