为特定应用程序框架创建gem时,我将gem中的项目嵌套在test目录中。例如,使用Rails特定的gem我会设置一个目录结构,如:
Rakefile
Gemfile
attached.gemspec
lib/attached.rb
lib/...
test/Gemfile
test/app/...
test/...
要测试,我使用gem 'attached', path: '...'
设置嵌套项目Gemfile,并在测试目录中运行rake test
。是否可以将一个任务添加到我的主Rakefile中,这将允许我在子项目中运行测试而无需先更改到目录中?
答案 0 :(得分:1)
我总是使用gem enginex
来帮助我使用集成的rails应用程序设置我的宝石。
在根Rakefile
中,他们写道:
require 'rake/testtask'
Rake::TestTask.new(:test) do |t|
t.libs << 'lib'
t.libs << 'test'
t.pattern = 'test/**/*_test.rb'
t.verbose = false
end
task :default => :test
他们不使用嵌套的Gemfile
,而是在test_helper.rb
内加载Rails项目,rails是gem的开发/测试依赖项。
看看宝石:
gem install enginex
或查看source。这个gem作为新的插件生成器包含在rails 3.1中。
答案 1 :(得分:0)
最简单的方法(我认为最不容易出错),就是有一个像:
这样的任务task :test do
system('cd test; bundle exec rake test')
end
另一种更复杂的方法是在根Rakefile中包含所有“子任务”,为每一个添加一个先决条件,这将改变当前目录,如下所示:
task :change_dir do
puts 'changing dir'
Dir.chdir('test')
end
namespace :sub do
load 'test/Rakefile'
end
Rake::Task.tasks.select{|t| t.name.start_with?('sub:')}.each do |task|
task.prerequisites.insert(0, 'change_dir')
end
我不确定这对捆绑器的效果如何。
我建议你做的最后一件事是看看bundle gem
命令创建的项目结构。我现在把它用于我的所有宝石,我相信利用它会让你的问题完全消失:)