Project

General

Profile

Actions

Feature #3592

open

Add loader for plugin and core fixtures

Added by liaham 21 days ago.

Status:
New
Priority:
Normal
Assignee:
Target version:
Start date:
Due date:
% Done:

0%

Estimated time:
Compatible Redmine Version:
6.0.z
Requires Documentation?:

Description

module AdvancedPluginHelper
  module FixtureLoader
   def fixtures(*table_names)
      if table_names.first == :all
        super(:all)
        create_plugin_fixtures(:all)
      else
        super(table_names)
        create_plugin_fixtures(table_names)
      end
    end

    def create_plugin_fixtures(table_names)
      plugin_fixture_path = "#{File.dirname __FILE__}/fixtures/"
      if table_names == :all
        table_names = Dir[::File.join(plugin_fixture_path, "{**,*}/*.{yml}")].uniq
        table_names.map! { |f| f[plugin_fixture_path.to_s.size..-5].delete_prefix("/") }
      end
      table_names.each do |table|
        ActiveRecord::FixtureSet.create_fixtures plugin_fixture_path, table if File.exist? "#{plugin_fixture_path}/#{table}.yml"
      end
      self.fixture_table_names |= table_names
      setup_fixture_accessors(table_names)
    end
  end
end

Usage:

module MyPlugin
 class UnitTest < ActiveSupport::TestCase
   extend AdvancedPluginHelper::FixtureLoader
 end
end

# Load all plugin and all core fixtures
module MyPlugin
  class MyModelTest < UnitTest
    fixtures :all
  end
end

# Load only listed fixtures
module MyPlugin
  class MyModelTest < UnitTest
    fixtures :core_fixture1, :core_fixture2, :plugin_fixture1, :plugin_fixture2
  end
end

No data to display

Actions

Also available in: Atom PDF