Test Rake Tasks

后端 未结 6 947
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-31 18:28

I am developing a ROR app that relies on many custom Rake tasks.

What is the best way to test them?

6条回答
  •  粉色の甜心
    2021-01-31 19:01

    I like the PivotalLabs' way the most. However, I generalized and modified their code as followed in my app:

    # file: spec/tasks/my_rake_file_spec.rb
    require 'spec_helper'
    require 'rake'
    
    describe 'my_rake_file.rake' do
      before :all do
        Rake.application.rake_require 'tasks/my_rake_file'
        Rake::Task.define_task(:environment)
      end
    
      let(:run_rake_task) {
        Rake::Task[task_name].reenable
        Rake.application.invoke_task task_name
      }
    
      describe 'my_task_name' do
        let(:task_name) { "my_task_name" }
    
        it "creates 10 cars" do
          run_rake_task
          Car.count.should == 10
        end
      end
    end
    

    Also, I extracted the heavy lifting of my_rake_file.rake's code to a module, which is stored in lib/

提交回复
热议问题