如何为 Redmine 3 插件创建功能测试

How to create functional tests for a Redmine 3 plugin

我创建了几个 Redmine 3 插件,现在我想编写测试代码以确保更好的稳定性。

但现在我无法创建任何有效的东西,我试图查看其他插件的测试,但我无法以任何可能的方式重现这些东西。

你们可以给我一个简单的canvas例子来说明如何进行吗?

我实际上创建了 redmine/plugins/redmine_timebank/test/functional/timebank_helper_test.rb 包含这个:

require 'redmine'
require 'versions_controller'

class TimebankHelperTest < ActionController::TestCase
  fixtures :projects,
           :issues,
           :issue_statuses,
           :versions,
           :trackers,
           :projects_trackers,
           :issue_categories,
           :time_entries

  def setup
    @controller = VersionsController.new
    @request    = ActionController::TestRequest.new
    @response   = ActionController::TestResponse.new
    User.current = User.where(:admin => true).first
    @request.session[:user_id] = User.current.id
    @project = Project.find(1)
  end

  def test_timebank_table
    puts "AWWWWWWWWWWWWWWWWWWWW YEAHHHHHHHHHHHHHHHHHHHHH !"
  end

end

但是当我执行命令时:

RAILS_ENV=test bin/rake redmine:plugins:test --trace

我收到这些错误:

** Execute redmine:plugins:test:functionals
/usr/bin/ruby2.3 -I"lib:test"  "/usr/lib/ruby/vendor_ruby/rake/rake_test_loader.rb" "plugins/*/test/functional/**/*_test.rb" 
/usr/share/redmine/lib/redmine/core_ext/active_record.rb:18:in `<top (required)>': uninitialized constant ActiveModel (NameError)
    from /usr/share/redmine/lib/redmine/core_ext.rb:1:in `require'
    from /usr/share/redmine/lib/redmine/core_ext.rb:1:in `block in <top (required)>'
    from /usr/share/redmine/lib/redmine/core_ext.rb:1:in `each'
    from /usr/share/redmine/lib/redmine/core_ext.rb:1:in `<top (required)>'
    from /usr/share/redmine/lib/redmine.rb:18:in `require'
    from /usr/share/redmine/lib/redmine.rb:18:in `<top (required)>'
    from /usr/share/redmine/plugins/redmine_timebank/test/functional/timebank_helper_test.rb:1:in `require'
    from /usr/share/redmine/plugins/redmine_timebank/test/functional/timebank_helper_test.rb:1:in `<top (required)>'
    from /usr/lib/ruby/vendor_ruby/rake/rake_test_loader.rb:10:in `require'
    from /usr/lib/ruby/vendor_ruby/rake/rake_test_loader.rb:10:in `block (2 levels) in <main>'
    from /usr/lib/ruby/vendor_ruby/rake/rake_test_loader.rb:9:in `each'
    from /usr/lib/ruby/vendor_ruby/rake/rake_test_loader.rb:9:in `block in <main>'
    from /usr/lib/ruby/vendor_ruby/rake/rake_test_loader.rb:4:in `select'
    from /usr/lib/ruby/vendor_ruby/rake/rake_test_loader.rb:4:in `<main>'
rake aborted!

看到 uninitialized constant ActiveModel (NameError) 让我觉得我需要更多依赖项,但我很难说我只需要 remine 来导入其余部分。

其他插件似乎在他们的 test/test_helper.rb 中导入了一些东西:

require File.expand_path(File.dirname(__FILE__) + '/../../../test/test_helper')

并将此行放入每个测试文件中:

require File.dirname(__FILE__) + '/../test_helper'

Rake 测试表明可以找到 none 个导入文件。如:

/usr/share/redmine/plugins/redmine_timebank/test/test_helper.rb:1:in `require': cannot load such file -- /usr/share/redmine/test/test_helper (LoadError)

我需要将哪些东西导入到我的功能测试中才能使其正常工作?

这个test/test_helper还存在吗?我在哪里可以找到它?

非常感谢,祝您有愉快的一天!

我假设您有一个名为 redmine_foo 的插件想要测试。

你看到其他插件做的是正确的,包括 Redmine 的 test/test_helper

require File.expand_path(File.dirname(__FILE__) + '/../../../test/test_helper')

redmine_foo/test/test_helper.rb.

如果你的 Redmine 没有 test/test_helper.rb,这肯定是有问题/缺失的迹象。我不确定 运行 测试位于 /usr/share 的 Redmine 是否是个好主意,所以首先,从 github 或官方 SVN 上正确检查 Redmine redmine.org,将您的插件添加到 plugins 目录(即在名为 plugins/redmine_foo 的文件夹中)并使用它。

在您的测试用例中,需要您的插件的测试助手(上面的 one-liner),即在 redmine_foo/test/functional/foos_controller_test.rb 中执行:

require File.expand_path('../../test_helper', __FILE__)

class FoosControllerTest < ActionController::TestCase
  setup do
    # setup stuff specific to your application / test case
  end

  test 'should show hello world' do
    get :show
    assert_response :success
    assert_match /hello world/, response.body
  end
end

如果您的测试用例继承自 ActionController::TestCase.

,则无需初始化任何 @controller@request@response 实例变量

要获得最简单的 Redmine 插件,上面的测试 运行 成功,你所要做的就是创建控制器,添加路由和基本 init.rb:

redmine_foo/app/controllers/foos_controller:

class FoosController < ApplicationController
  def show
    render text: 'hello world'
  end
end

redmine_foo/init.rb:

Redmine::Plugin.register :redmine_foo do
  name 'Redmine Foo'
  version '1.0.0'
end

redmine_foo/config/routes.rb:

resource :foo

运行 使用 NAME=redmine_foo bin/rake redmine:plugins:test

从 Redmine 基本目录进行测试