红宝石 – 在rspec(和黄瓜)中测试耙子任务

我是 Ruby的新手,我一直在尝试学习Rake,RSpec和Cucumber.我发现一些代码可以帮助我测试我的Rake任务,但我遇到麻烦让它工作.我被告知在这里: http://blog.codahale.com/2007/12/20/rake-vs-rspec-fight/放下:
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 in ‘Rake task
install_grapevine should install to
the mygrapevine directory’

wrong number of arguments (1 for 2)
/spec/spec_helper.rb: 21:in instance_eval'
/spec/spec_helper.rb: 21:in
block in invoke!’
/spec/spec_helper.rb: 20:in each'
/spec/spec_helper.rb: 20:in
invoke!’
/spec/tasks/rakefile_spec.rb:12:in `block (2 levels) in

不幸的是,我刚刚在一个星期的红宝石下腰带,所以metaprogramming的东西在我的头上.有人可以指出我的方向正确吗?

解决方法

这对我有用(Rails3 / Ruby 1.9.2)
When /^the system does it's automated tasks$/ do    
  require "rake"
  @rake = Rake::Application.new
  Rake.application = @rake
  Rake.application.rake_require "tasks/cron"
  Rake::Task.define_task(:environment)
  @rake['cron'].invoke   
end

在这里替换你的耙子任务名称,并且还要注意,如果你的加载路径中没有lib文件夹,你的要求可能是“lib / tasks / cron”.

我同意,您只应该在耙子任务中做最少的工作,并将其余部分推送到模型以便于测试.话虽如此,我认为重要的是确保在我的集成测试中,我的cron任务中的代码实际运行,所以我认为耙子任务的非常温和的测试是合理的.

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...