Created
February 23, 2022 11:07
-
-
Save stephancom/fecd69d8710882ea6e493c001ecee9e8 to your computer and use it in GitHub Desktop.
spec/support/tasks.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://www.eliotsykes.com/test-rails-rake-tasks-with-rspec | |
require 'rake' | |
# Task names should be used in the top-level describe, with an optional | |
# "rake "-prefix for better documentation. Both of these will work: | |
# | |
# 1) describe "foo:bar" do ... end | |
# | |
# 2) describe "rake foo:bar" do ... end | |
# | |
# Favor including "rake "-prefix as in the 2nd example above as it produces | |
# doc output that makes it clear a rake task is under test and how it is | |
# invoked. | |
module TaskExampleGroup | |
extend ActiveSupport::Concern | |
included do | |
let(:task_name) { self.class.top_level_description.sub(/\Arake /, '') } | |
let(:tasks) { Rake::Task } | |
# Make the Rake task available as `task` in your examples: | |
subject(:task) { tasks[task_name] } | |
end | |
end | |
RSpec.configure do |config| | |
# Tag Rake specs with `:task` metadata or put them in the spec/tasks dir | |
config.define_derived_metadata(file_path: %r{/spec/tasks/}) do |metadata| | |
metadata[:type] = :task | |
end | |
config.include TaskExampleGroup, type: :task | |
config.before(:suite) do | |
Rails.application.load_tasks | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment