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

时间:2017-02-01 20:15:57

标签: redmine functional-testing redmine-plugins

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

但是现在我无法创造任何有效的东西,我试着看看其他插件的测试,我无法以任何可能的方式重现事物。

你们可以给我一个如何进行的简单画布示例吗?

我实际创建了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)让我觉得我需要更多的依赖,但我很难说我只需要redmine来导入其余的所有。

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

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

这一行进入每个测试文件:

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

Rake测试表明,找不到任何导入文件。如:

/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是否仍然存在?我在哪里可以找到它?

非常感谢你,祝你有个美好的一天!

1 个答案:

答案 0 :(得分:2)

我假设您有一个名为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或redmine.org上的官方SVN获取Redmine的正确结帐,添加您的插件到plugins目录(即名为plugins/redmine_foo的文件夹中)并使用它。

在您的测试用例中,需要插件的测试助手(上面的单行),即在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

如果您的测试用例继承自@controller,则无需初始化任何@request@responseActionController::TestCase实例变量。

要获得最简单的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基本目录运行测试