[英]Nested Project For Testing Ruby Gem
為特定的應用程序框架創建gem時,我將一個項目嵌套在test目錄中的gem內。 例如,使用特定於Rails的gem,我將設置一個目錄結構,例如:
Rakefile
Gemfile
attached.gemspec
lib/attached.rb
lib/...
test/Gemfile
test/app/...
test/...
為了進行測試,我使用gem 'attached', path: '...'
設置了嵌套項目Gemfile gem 'attached', path: '...'
並在測試目錄中運行rake test
。 是否可以在我的主Rakefile中添加一個任務,使我可以在子項目中運行測試而無需先更改到目錄中?
我始終使用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
或檢查來源 。 此gem作為新的插件生成器包含在rails 3.1中。
最簡單的方法(而且我認為最不容易出錯)是執行以下任務:
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
命令創建的項目結構。 我現在將其用於我的所有寶石,並且我相信利用它可以使您的問題完全消失:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.