我是Ruby的新手,我一直在尝试学习Rake,RSpec和Cucumber。我发现了一些代码,可以帮助我测试我的Rake任务,但是我无法使其工作。我在这里告诉:http://blog.codahale.com/2007/12/20/rake-vs-rspec-fight/放弃这样的:在rspec(和黄瓜)中测试rake任务
def describe_rake_task(task_name, filename, &block)
require "rake"
describe "Rake task #{task_name}" do
attr_reader :task
before(:all) do
@rake = Rake::Application.new
Rake.application = @rake
load filename
@task = Rake::Task[task_name]
end
after(:all) do
Rake.application = nil
end
def invoke!
for action in task.instance_eval { @actions }
instance_eval(&action)
end
end
instance_eval(&block)
end
end
到我spec_helper.rb文件。
我已经成功了利用这个代码,并在我的黄瓜的步骤是这样运行:
When /^I run the update_installers task$/ do
@rake = Rake::Application.new
Rake.application = @rake
load "lib/tasks/rakefile.rb"
@task = Rake::Task["update_installers"]
for action in @task.instance_eval { @actions }
instance_eval(&action)
end
instance_eval(&block)
Rake.application = nil
end
但是当我试图把事情在rspec的工作,我得到以下错误。
引发ArgumentError在 'Rake任务 install_grapevine应该安装到 的mygrapevine目录'
错误的参数数目(1 2) /spec/spec_helper.rb:21:在
instance_eval' /spec/spec_helper.rb: 21:in
块调用! “ /spec/spec_helper.rb:20:在each' /spec/spec_helper.rb: 20:in
invoke!' /spec/tasks/rakefile_spec.rb:12:in`块(2级)在 '
不幸的是,我刚下通过带红宝石的下一个星期了,所以元编程的东西是在我的头。任何人都可以将我指向正确的方向吗?
相同没有RSpec:http://stackoverflow.com/questions/3530/how-do-i-rake-tasks-within-a-ruby-script – 2014-09-24 12:57:52