Is there a way to monkey-patch rails tests from plugins?

Added by Manuel Vázquez Acosta almost 3 years ago

Me and my team have been heavily producing plugins for Redmine (currently 1.1 branch). So far, we have contained from polluting Redmine's base code by writing most of our changes as rails plugins. This has two main benefits:

  • Most of the time, integration with newer Redmine versions is straightforward.
  • We can enable/disable some custom behavior by dropping/removing a plugin. In fact, this is a key factor for us, because our clients have quite disparate requirements.

Some of our plugins monkey-patch Redmine's classes. For instance, we have a plugin that "enforces" stricter validations to the `Issue` model: `start_date`, `due_date` and `estimated_hours` are required for issues where `leaf?` is true.

This monkey-patching stuff makes several tests fail and/or raise exceptions. This very plugin, for instance, makes the `create_issue!` method from `IssueNestedSetTest` class to create invalid issues (i.e. lacking the required attributes the plugin is enforcing):

# Helper that creates an issue with default attributes
def create_issue!(attributes={})
  Issue.create!({:project_id => 1, :tracker_id => 1, :author_id => 1, :subject => 'test'}.merge(attributes))
end

Since the plugin may or may not be active, we would not like to change the test itself. We think it's best the plugin monkey-patch the test class accordingly:

  module StandardTestPatches
    module InstanceMethods
      def create_issue_with_gespro_standards!(attributes={})
        attributes.merge!(:start_date => 1.day.ago, :due_date => 1.day.from_now, :estimated_hours => 8)
        create_issue_without_gespro_standards!(attributes)
      end
    end
    def self.included(base)
      base.send :include, InstanceMethods
      base.class_eval do
        alias_method_chain :create_issue!, :gespro_standards
      end
    end
  end

However since our plugin's `init.rb` file is required before the tests classes are loaded we can't monkey-patch the `IssueNestedSetTest` class there.

Is there a way to monkey-patch rails tests from plugins?

Replies (1)

(1-1/1)