跳至内容 跳至搜索
方法
F
S
U

实例公共方法

fixtures(*fixture_set_names)

# File activerecord/lib/active_record/test_fixtures.rb, line 56
def fixtures(*fixture_set_names)
  if fixture_set_names.first == :all
    raise StandardError, "No fixture path found. Please set `#{self}.fixture_paths`." if fixture_paths.blank?
    fixture_set_names = fixture_paths.flat_map do |path|
      names = Dir[::File.join(path, "{**,*}/*.{yml}")].uniq
      names.reject! { |f| f.start_with?(file_fixture_path.to_s) } if defined?(file_fixture_path) && file_fixture_path
      names.map! { |f| f[path.to_s.size..-5].delete_prefix("/") }
    end.uniq
  else
    fixture_set_names = fixture_set_names.flatten.map(&:to_s)
  end

  self.fixture_table_names = (fixture_table_names | fixture_set_names).sort
  setup_fixture_accessors(fixture_set_names)
end

set_fixture_class(class_names = {})

当无法从固定文件名称推断类名称时,设置固定文件的模型类。

示例

set_fixture_class some_fixture:        SomeModel,
                  'namespaced/fixture' => Another::Model

键必须是固定文件名称,与固定文件短路径相对应。

# File activerecord/lib/active_record/test_fixtures.rb, line 52
def set_fixture_class(class_names = {})
  self.fixture_class_names = fixture_class_names.merge(class_names.stringify_keys)
end

setup_fixture_accessors(fixture_set_names = nil)

# File activerecord/lib/active_record/test_fixtures.rb, line 72
def setup_fixture_accessors(fixture_set_names = nil)
  fixture_set_names = Array(fixture_set_names || fixture_table_names)
  unless fixture_set_names.empty?
    self.fixture_sets = fixture_sets.dup
    fixture_set_names.each do |fs_name|
      key = fs_name.to_s.include?("/") ? -fs_name.to_s.tr("/", "_") : fs_name
      key = -key.to_s if key.is_a?(Symbol)
      fs_name = -fs_name.to_s if fs_name.is_a?(Symbol)
      fixture_sets[key] = fs_name
    end
  end
end

uses_transaction(*methods)

防止自动将每个指定的测试包装在事务中,以允许应用程序逻辑事务在顶级(非嵌套)上下文中进行测试。

# File activerecord/lib/active_record/test_fixtures.rb, line 88
def uses_transaction(*methods)
  @uses_transaction = [] unless defined?(@uses_transaction)
  @uses_transaction.concat methods.map(&:to_s)
end

uses_transaction?(method)

# File activerecord/lib/active_record/test_fixtures.rb, line 93
def uses_transaction?(method)
  @uses_transaction = [] unless defined?(@uses_transaction)
  @uses_transaction.include?(method.to_s)
end