(apparently) identical tests for two rake tasks; only one passes

I am trying to write tests in rspec for two rake tasks that are defined in a single file (in a Rails 3.0.11 project). For some reason, only one of them passes. I wrote a small demo to abstract the actual contents of the tasks, and the same thing happens. Both tasks work when called rakefrom the command line. What's happening? Here is my demo:

Lib / tasks / demo_tasks.rake

namespace :demo do
  task :test => :environment do
    puts "test!"
  end

  task :test_two => :environment do
    puts "second test!"
  end
end

Specifications / Library / Tasks / demo_spec.rb

require 'spec_helper'
require 'rake'

describe "test tasks" do
  let(:rake) do
    app = Rake::Application.new
    app.options.silent = true
    app
  end

  before :each do
    Rake.application = rake
    Rake.application.rake_require 'lib/tasks/demo_tasks',
                                  [Rails.root.to_s]
    Rake::Task.define_task :environment
  end

  describe "demo:test" do
    it "runs" do
      rake["demo:test"].invoke
    end
  end

  describe "demo:test_two" do
    it "also_runs" do
      rake["demo:test_two"].invoke
    end
  end
end

rspec spec / lib / tasks / demo_spec.rb

test tasks
  demo:test
test!
    runs
  demo:test_two
    also_runs (FAILED - 1)

Failures:

  1) test tasks demo:test_two also_runs
     Failure/Error: rake["demo:test_two"].invoke
     RuntimeError:
       Don't know how to build task 'demo:test_two'
     # ./spec/lib/tasks/demo_spec.rb:26:in `block (3 levels) in <top (required)>'
+5
source share
2 answers

A bit: Change beforeto before :all(instead :each).

: rake_require.

Rake.application.rake_require 'lib/tasks/demo_tasks', 
                              [Rails.root.to_s], 
                              []

def rake_require(file_name, paths=$LOAD_PATH, loaded=$")
  fn = file_name + ".rake"
  return false if loaded.include?(fn)
  ...

$" - Ruby, , require.

, rake_require , Ruby. , : Ruby , , , , Ruby, rake .

before :all , , let : , , .

, ? - , .

:

describe "test tasks" do
  before :all do
    Rake.application = Rake::Application.new
    Rake.application.rake_require 'lib/tasks/demo_tasks', [Rails.root.to_s]
    Rake::Task.define_task :environment
  end

  describe "demo:test" do
    it "runs" do
      Rake::Task["demo:test"].invoke
    end
  end
end

before, Rake::Task:

before :all do
  @rake = Rake::Application.new
  Rake.application = @rake
  Rake.application.rake_require 'lib/tasks/demo_tasks', [Rails.root.to_s]
  Rake::Task.define_task :environment
end

describe "demo:test" do
  it "runs" do
    @rake["demo:test"].invoke

, . , .

+6

, , #invoke . @dave-newtown.

, (Rake v12) #invoke , :

RSpec.describe "demo:test" do
  it "runs" do
    expect(SomethingWeAreInvoking).to eql(ProofIfWasInvoked)
    Rake::Task["demo:test"].invoke
  end

  it "runs" do
    expect(SomethingWeAreInvoking).to eql(ProofIfWasInvoked)
    Rake::Task["demo:test"].invoke
  end
end

... , it , , it, Rake.application, #invoke . " ", , Rake.application.

, , , , Rake v12, -, , Rake (fo) , .

Rake #execute, , Rake, .

:

require 'spec_helper'
require 'rake'

RSpec.describe 'demo:test' do
  before :each do
    Rake.application = Rake::Application.new
    Rake.application.rake_require 'lib/tasks/demo_tasks', [Rails.root.to_s], []
    Rake::Task.define_task(:environment)
  end

  it 'runs' do
    expect(SomethingWeAreInvoking).to eql(ProofIfWasInvoked)
    Rake.application.invoke_task('demo.test')
  end

  it 'runs with a parameter' do
    expect(SomethingWeAreInvoking).to eql(ProofIfWasInvoked)
    Rake.application.invoke_task('demo.test[42]')
  end
end
  • before :each.
  • Rake.application; , invoke , .
  • Rake.application , Rake.application.rake_require 'tasks/demo_tasks', .. , Rake , " "" , "" @dave-newtown.
  • Rake.application.invoke_task Rake::Task[...].invoke. , Rake, , , "" , .

, , , , Rake v12, -, , Rake, , . , Rake , .

, - .

Reference articles:

( : rake rspec test )

0

All Articles