How to test system calls from the rake task

Andrew Kozin

Suppose I have a pretty simple Rake task:

task :test do
  system "bundle exec rspec spec"
end

I try testing it by stubbing the ::Kernel.system method call:

describe "test" do

  before { allow(::Kernel).to receive(:system) }

  it "runs 'bundle exec rspec spec'" do
    expect(::Kernel).to receive(:system).with "bundle exec rspec spec"
    Rake::Task[:test].invoke
  end
end

But the method seems not to be stubbed at all. Instead, I run into the infinite cycle of iterations calling the test suite.

What's wrong with it, and howto stub system calls properly?

Łukasz Adamczak

Note that Kernel is a module which is included into every ruby Object. And Kernel#system is an instance method (not a class method).

One solution (although discouraged by rspec maintainers) is to use "Any instance":

it "runs 'bundle exec rspec spec'" do
  expect_any_instance_of(Kernel).to receive(:system).with "bundle exec rspec spec"
  Rake::Task[:test].invoke
end

In order to use regular expect or allow, you will need the actual instance of the object which is receiving the message. For a Rake task this will be cumbersome (although not impossible - see this question) - they are executed in the toplevel context.

I would propose that you encapsulate your system calls into utility class methods and expect those. It would make the testing easier and you have explicit classes & instances to work with.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to test rake task in Minitest?

From Dev

Test tasks missing from rake: rake aborted! Don't know how to build task 'test:units'

From Dev

How to test rake task when arguments are passed

From Dev

How to design a rake task that calls multiple class methods?

From Dev

Is there any better way to execute several system commands from a rake task?

From Dev

Executing a rake task after `rake test`

From

How do I return early from a rake task?

From Dev

Slow performance from rake task - How to streamline these Rails queries

From Dev

How to run db:migrate from another rake task with parameters?

From Dev

Running foreman from a rake task

From Dev

How to check status with a rake task

From Dev

How are system calls from man 2 invoked?

From Dev

Access Paperclip attachments from a rake task

From Dev

send GET request from rake task

From Dev

Check if rake task exists from within Rakefile

From Dev

Run rake task from Capistrano if it exists

From Dev

Build db:rake task from existing Database

From Dev

Unable to make an API call from Rake task

From Dev

Run two ruby scripts from rake task

From Dev

How to pass all system properties to test task in gradle kotlin dsl?

From

How to pass arguments into a Rake task with environment in Rails?

From Dev

How to run a rake task on jenkins with string parameters?

From Dev

How to use in rake task ActiveRecord model and argument

From Dev

Rspec Rake Task: How to parse a parameter?

From

How to pass command line arguments to a rake task

From Dev

how to run rake task using rails migration

From Dev

How to catch raised exception in rake task

From Dev

How to run a rake task depending on environment?

From Dev

How do I build a Jekyll site from Rake task without using the command line?

Related Related

  1. 1

    How to test rake task in Minitest?

  2. 2

    Test tasks missing from rake: rake aborted! Don't know how to build task 'test:units'

  3. 3

    How to test rake task when arguments are passed

  4. 4

    How to design a rake task that calls multiple class methods?

  5. 5

    Is there any better way to execute several system commands from a rake task?

  6. 6

    Executing a rake task after `rake test`

  7. 7

    How do I return early from a rake task?

  8. 8

    Slow performance from rake task - How to streamline these Rails queries

  9. 9

    How to run db:migrate from another rake task with parameters?

  10. 10

    Running foreman from a rake task

  11. 11

    How to check status with a rake task

  12. 12

    How are system calls from man 2 invoked?

  13. 13

    Access Paperclip attachments from a rake task

  14. 14

    send GET request from rake task

  15. 15

    Check if rake task exists from within Rakefile

  16. 16

    Run rake task from Capistrano if it exists

  17. 17

    Build db:rake task from existing Database

  18. 18

    Unable to make an API call from Rake task

  19. 19

    Run two ruby scripts from rake task

  20. 20

    How to pass all system properties to test task in gradle kotlin dsl?

  21. 21

    How to pass arguments into a Rake task with environment in Rails?

  22. 22

    How to run a rake task on jenkins with string parameters?

  23. 23

    How to use in rake task ActiveRecord model and argument

  24. 24

    Rspec Rake Task: How to parse a parameter?

  25. 25

    How to pass command line arguments to a rake task

  26. 26

    how to run rake task using rails migration

  27. 27

    How to catch raised exception in rake task

  28. 28

    How to run a rake task depending on environment?

  29. 29

    How do I build a Jekyll site from Rake task without using the command line?

HotTag

Archive