How to disconnect / send a command line call using rspec?

I am trying to check the output from a command line tool. How can I "fake" a command line call using rspec? Doing the following does not work:

it "should call the command line and return 'text'" do
  @p = Pig.new
  @p.should_receive(:run).with('my_command_line_tool_call').and_return('result text')
end

How to create this stub?

+5
source share
3 answers

Here is a quick example that I made. I call ls from my dummy class. Tested with rspec

require "rubygems"
require "spec"

class Dummy
  def command_line
    system("ls")
  end
end

describe Dummy do
  it  "command_line should call ls" do
    d = Dummy.new
    d.should_receive("system").with("ls")
    d.command_line
  end
end
+6
source

Using the new message wait syntax :

specifications / dummy _spec.rb

require "dummy"

describe Dummy do
  it "command_line should call ls" do
    d = Dummy.new
    expect(d).to receive(:system).with("ls")
    d.command_line
  end
end

Library / dummy.rb

class Dummy
  def command_line
    system("ls")
  end
end
+11
source

, Kernel:

module Kernel
  def system(cmd)
    "call #{cmd}"
  end
end

> system("test")
=> "call test" 

: Mock

-5
source

All Articles