DEV Community

Monica
Monica

Posted on

1 2

TIL about testing commands

This is a walk-with-me-while-I-do-things series, I'm going to tell you what I learned while I'm learning

If this was our program

# my_tool.rb

# Removes and returns the first item from the command-line arguments
command = ARGV.shift

puts command

you can test this code using RSpec with

# specs/my_tool_spec.rb

RSpec.describe "my_tool" do
  it "says hello" do
    expect { system "ruby my_tool.rb hello" }.
      to output("hello\n").
      to_stdout_from_any_process
  end
end

Here we have three fundamental pieces:

  1. the method Kernel#system that invokes a command as if we were on the command line itself. The argument passed as a string will be the command being executed;

  2. the RSpec matcher output that checks against the expectation and passes if the expectation block on which it is invoked outputs to_stdout or to_stderr the string passed as an argument;

  3. the to_stdout_from_any_process that we have to use instead of to_sdtdout because the system call is actually spawning a subprocess, and RSpec wouldn't be able to access $stdout otherwise.

If we weren't testing a command line invocation, we could have written

expect { print "Same process" }.
  to output("Same process").
  to_stdout

More sources are here. To use RSpec for capturing the output and where you can go to the official doc.

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay