Module: RSpecCommand::Rake

Defined in:
lib/rspec_command/rake.rb

Overview

An RSpec helper module for testing Rake tasks without running them in a full subprocess. This improves test speed while still giving you most of the benefits of integration testing.

Examples:

RSpec.configure do |config|
  config.include RSpecCommand::Rake
end

Enable for a single example group

describe 'mytask' do
  rakefile <<-EOH
    ...
  EOH
  rake_task 'mytask'
  its(:stdout) { it_expected.to include('1.0.0') }
end

Since:

  • 1.0.0

Class Method Summary collapse

Class Method Details

.rake_task(name, *args)

This method returns an undefined value.

Run a Rake task as the subject of this example group. The subject will be a string returned by RSpecCommand#capture_output.

Examples:

describe 'mytask' do
  rakefile 'require "myapp/rake_tasks"'
  rake_task 'mytask'
  its(:stdout) { is_expected.to include 'Complete!' }
end

Parameters:

  • name (String)

    Name of the task to execute.

  • args (Array<Object>)

    Arguments to pass to the task.

Since:

  • 1.0.0



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/rspec_command/rake.rb', line 57

def rake_task(name, *args)
  [:rake] = true
  subject do
    exitstatus = []
    capture_output do
      Process.waitpid fork {
        # This has to be nocov because simplecov doesn't track across fork.
        # :nocov:
        # Defang SimpleCov so it doesn't print its stuff. Can be removed
        # when https://github.com/colszowka/simplecov/pull/377 is in a
        # released version.
        if defined?(SimpleCov)
          SimpleCov.at_exit { SimpleCov.instance_variable_set(:@result, nil) }
        end
        # Because #init reads from ARGV and will try to parse rspec's flags.
        ARGV.replace([])
        Dir.chdir(temp_path)
        ENV.update(_environment)
        rake = ::Rake::Application.new.tap do |rake|
          ::Rake.application = rake
          rake.init
          rake.load_rakefile
        end
        rake[name].invoke(*args)
      }
      exitstatus << $?.exitstatus
      # :nocov:
    end.tap do |output|
      output.define_singleton_method(:exitstatus) { exitstatus.first }
    end
  end
end

.rakefile(content = nil, &block)

This method returns an undefined value.

Write out a Rakefile to the temporary directory for this example group. Content can be passed as either a string or a block.

Examples:

describe 'mytask' do
  rakefile <<-EOH
task 'mytask' do
  ...
end
EOH
  rake_task 'mytask'
  its(:stdout) { is_expected.to include 'Complete!' }
end

Parameters:

  • content (String) (defaults to: nil)

    Rakefile content.

  • block (Proc)

    Optional block to return the Rakefile content.

Since:

  • 1.0.0



106
107
108
# File 'lib/rspec_command/rake.rb', line 106

def rakefile(content=nil, &block)
  file('Rakefile', content, &block)
end