【问题标题】:Write rspec for rake task为 rake 任务编写 rspec
【发布时间】:2016-06-14 03:30:28
【问题描述】:

我有这样的任务/lib/crawler.rake:

namespace :crawler do
  area_names = Dir[Rails.root.join("lib", "crawler", "*.rb")].map do |file_name|
    File.basename(file_name, ".rb")
  end

  area_names.each do |area_name|
    task area_name.to_sym => :environment do
      logger = Logger.new("log/crawl_#{area_name}.log")

      # do something

      parallel_results = crawler.crawl
      mutex = Mutex.new

      Parallel.each(parallel_results, in_threads: [parallel_results.count, CRAWL_CONFIG["building_thread_max"]].min) do |pages|
        begin
          # do something
        rescue => e
          # do something
          raise e
        end
      end

      Availability.update_by_grounds_and_time
    end
  end
end

这里的逻辑,如果并行后一切正常,我们将调用update_by_grounds_and_time方法更新Availability;如果出现错误,我们将停止操作并引发错误。

所以我想编写 rspec 来测试这些情况,我想在这里模拟/存根任务的输出(通过或引发错误)并检查我们是否调用了 update_by_grounds_and_time 方法? 我们可以不需要调用真正的任务吗?我们可以使用Rspec Mock吗?

你能帮帮我吗! 谢谢

【问题讨论】:

    标签: ruby-on-rails rspec rake-task rspec-mocks


    【解决方案1】:

    在这些情况下,我通常做的是将肉提取到单独的类/服务对象/任何东西中,这更容易测试。然后,rake 任务就变成了该对象的调用者,因此不需要进行测试。

    【讨论】:

      【解决方案2】:

      如果在Rakefile中定义,试试这个:

      require 'rake'
      
      RSpec.describe "Rake Tasks" do
        before do
          file, path = Rake.application.find_rakefile_location
          Rake.load_rakefile("#{path}/#{file}")
        end
      
        it "should invoke some tasks" do
          expect(Availability).to receive(:update_by_grounds_and_time)
          Rake.application["crawler:#{area_name}"].invoke
        end
      end
      

      如果是在foo.rake中定义的,那么试试这个:

      require 'rake'
      
      RSpec.describe "Rake Tasks" do
        before do
          Rake.application.rake_require('/path/to/lib/tasks/foo')
        end
      
        it "should invoke some tasks" do
          expect(Availability).to receive(:update_by_grounds_and_time)
          Rake.application["crawler:#{area_name}"].invoke
        end
      end
      

      更新(错误情况)

      例如

      # foo.rake
      Parallel.each(parallel_results, in_threads: [parallel_results.count, CRAWL_CONFIG["building_thread_max"]].min) do |pages|
        begin
          foo = Foo.new
          foo.bar
          # do something else
        rescue => e
          # do something
          raise e
        end
      end
      
      # foo_spec.rb
      require 'rake'
      
      RSpec.describe "Rake Tasks" do
        before do
          Rake.application.rake_require('/path/to/lib/tasks/foo')
        end
      
        it "should not call Availability#update_by_grounds_and_time if error raised" do
          allow_any_instance_of(Foo).to receive(:bar).and_raise(StandardError)
          expect(Availability).to_not receive(:update_by_grounds_and_time)
          expect { Rake.application["crawler:#{area_name}"].invoke }.to raise_error(StandardError)
        end
      end
      

      【讨论】:

      • 谢谢@scorix。但是我可以不需要调用一个真正的任务吗?如果我们抛出错误(跳转到rescue 块),我们将停止操作并且不运行Availability.update_by_grounds_and_time 行。所以我想在这里检查2个案例:如果通过=>我在运行任务后调用update_by_grounds_and_time,如果引发错误=>我没有调用
      • 当我尝试在错误情况下运行时,rpsec 返回失败如下:Failure/Error: Rake.application["crawler:#{area_name}"].invoke StandardError: StandardError?我们可以通过这个案子吗?
      • 对不起,这是我的错误。已更新。
      • 啊,我需要在before 块中通过Rake::Task.define_task(:environment) 定义任务吗?
      猜你喜欢
      • 2015-12-29
      • 1970-01-01
      • 2018-10-17
      • 1970-01-01
      • 2011-09-08
      • 1970-01-01
      • 1970-01-01
      • 2015-01-24
      • 1970-01-01
      相关资源
      最近更新 更多