Testing plugins in Redmine 2.x

Added by Harry Garrood almost 10 years ago

I am trying to write tests for my redmine 2.x plugin. I've read http://www.redmine.org/projects/redmine/wiki/Plugin_Tutorial#Testing-your-plugin, and have created the following files in test/:

  • test_helper.rb
  • fixtures/release_notes.yml
  • unit/release_note_test.rb

Then, in release_note_test.rb, i call:

fixtures 'release_notes'

Unfortunately, this results in:

Errno::ENOENT: No such file or directory - /home/harry/code/redmine/test/fixtures/release_notes.yml

It seems Redmine is looking in its own fixtures directory, and not my plugin's. Is there a good way of telling it to use the plugin's fixtures? I've considered adding code to copy fixtures to Redmine's own location in test_helper.rb but that feels very naughty...

Replies (9)

RE: Testing plugins in Redmine 2.x - Added by Harry Garrood almost 10 years ago

I think I've fixed this, by using this in my test_helper.rb:

# Load the normal Rails helper
require File.expand_path(File.dirname(__FILE__) + '/../../../test/test_helper')

# Ensure that we are using the temporary fixture path
class ActiveSupport::TestCase
  self.fixture_path = File.dirname(__FILE__) + '/fixtures'
end

RE: Testing plugins in Redmine 2.x - Added by Kirill Bezrukov (RedmineUP) almost 10 years ago

It seems this replace redmine fixtures and you will not be able to load redmine objects like issues for your tests. ActiveSupport::TestCase.fixture_path = File.dirname(FILE) + '/fixtures'

RE: Testing plugins in Redmine 2.x - Added by Harry Garrood almost 10 years ago

Yes, that's true. For the benefit of anyone else -- I've also copied issues.yml from redmine's fixtures directory into my plugin's fixtures directory to get around that.

RE: Testing plugins in Redmine 2.x - Added by Kirill Bezrukov (RedmineUP) almost 10 years ago

For me it was solved by that code:

class ContactsControllerTest < ActionController::TestCase  
  fixtures :projects,
           :users,
           :roles,
           :members,
           :member_roles,
           :issues,
           :issue_statuses,
           :versions,
           :trackers,
           :projects_trackers

    ActiveRecord::Fixtures.create_fixtures(File.dirname(__FILE__) + '/../fixtures/', 
                            [:contacts,
                             :contacts_projects,
                             :contacts_issues,
                             :deals,
                             :notes,
                             :roles,
                             :enabled_modules,
                             :tags,
                             :taggings,
                             :contacts_queries])   

RE: Testing plugins in Redmine 2.x - Added by Akiko Takano almost 10 years ago

Hello, Kirill.

Your code works fine for me, thank you so much!

Kirill Bezrukov wrote:

For me it was solved by that code:

[...]

Also I added this workaround in my plugin's test_helper.rb.

require File.expand_path(File.dirname(__FILE__) + '/../../../test/test_helper')
ActiveRecord::Fixtures.create_fixtures(File.dirname(__FILE__) + '/fixtures/',
                                       [:my_plugin_fixture_1, :my_plugin_fixture_2])

RE: Testing plugins in Redmine 2.x - Added by Nicolas Zermati over 9 years ago

Hello,

I've packaged this feature in the plugin's test_helper.rb.

module Redmine
  module PluginFixturesLoader
    def self.included(base)
      base.class_eval do
        def self.plugin_fixtures(*symbols)
          ActiveRecord::Fixtures.create_fixtures(File.dirname(__FILE__) + '/fixtures/', symbols)
        end
      end
    end
  end
end

unless ActionController::TestCase.included_modules.include?(Redmine::PluginFixturesLoader)
  ActionController::TestCase.send :include, Redmine::PluginFixturesLoader
end

Then you can use the plugin_fixtures the same way you use fixtures.

RE: Testing plugins in Redmine 2.x - Added by Justin Leishman over 9 years ago

I've tried these suggestions, but changing the value of fixture_path and using #create_fixtures has not allowed me to use methods like #my_models(:some_fixture), which is what I expect.

Is anyone using the techniques described above with the class accessor methods, or is everyone accessing the fixtures another way?

RE: Testing plugins in Redmine 2.x - Added by Harry Garrood over 9 years ago

This doesn't really answer your question (I don't know the answer), but I gave up with fixtures and switched to factory_girl . I remember thinking it worked quite well for me. Definitely worth a try, at least.

RE: Testing plugins in Redmine 2.x - Added by Wesley Jones almost 7 years ago

Thanks Akiko Takano, that worked perfect. Justin, I thought it wasn't working either, but after you implement Akiko's solution, you must also include them in the fixtures declaration in your test class.

test_helper.rb

# Load the normal Rails helper
require File.expand_path(File.dirname(__FILE__) + '/../../../test/test_helper')
ActiveRecord::Fixtures.create_fixtures(File.dirname(__FILE__) + '/fixtures/', [:contracts, :user_contract_rates, :user_project_rates])

contracts_controller_test.rb

# include main and custom fixtures here
fixtures :issues, :projects, :users, :time_entries,
         :contracts, :user_contract_rates, :user_project_rates

def setup
    @contract = contracts(:contract_one)
    @project = projects(:projects_001)
    etc......

(1-9/9)