【问题标题】:Get the list of all fixture names defined in the project获取项目中定义的所有夹具名称的列表
【发布时间】:2014-10-30 08:43:05
【问题描述】:

我定义了几个夹具文件,它们由fixtures :all 语句加载。

如何获得项目中所有夹具名称的数组?无需通过 test/fixtures 文件夹搜索 *.yml 文件。

附:一个明显的解决方案是定义这样的方法:

def all_fixtures
    @all_fixtures ||= Dir.entries("#{Rails.root}/test/fixtures")
                         .select { |filename| /.*\.yml/.match(filename) }
                         .map{ |filename| filename[0..-5].to_sym }
end

但我相信还有更优雅的东西。

【问题讨论】:

    标签: ruby-on-rails fixtures minitest


    【解决方案1】:

    我不知道除了做你提到的任何其他方法。但是,您可以稍微清理一下该语句。

    直接从ActiveRecordfixutres.rb复制逻辑,可以做如下操作

    def all_fixtures
      # Get the fixture path that's configured
      fixture_path = ActiveSupport::TestCase.fixture_path
      # Get all YML files in all subdirectories
      fixture_set_names = Dir["#{fixture_path}/{**,*}/*.{yml}"]
      # Clip the name to get all the fixture names
      fixture_set_names.map! { |f| f[(fixture_path.to_s.size + 1)..-5] }
    end
    

    【讨论】:

      猜你喜欢
      • 2013-04-19
      • 2020-12-17
      • 1970-01-01
      • 2011-08-10
      • 2022-12-01
      • 1970-01-01
      • 2013-04-29
      • 2014-07-30
      • 2022-10-12
      相关资源
      最近更新 更多