Ruby Rspec与系统交互

Ruby Rspec与系统交互,ruby,testing,operating-system,mocking,rspec,Ruby,Testing,Operating System,Mocking,Rspec,我有一个程序,将运行几个系统命令。我知道的最简单的方法是使用``符号。我希望为这些工具构建测试,而不必在系统上运行这些系统命令 有没有办法模拟系统的响应 如果您的代码运行大量外部命令,您应该创建一个简单的包装器并模拟: def wants_to_run_external_command(command_runner) command_runner.run('echo "hello world"') end 然后像这样测试它: it 'echos "hello world"' do co

我有一个程序,将运行几个系统命令。我知道的最简单的方法是使用``符号。我希望为这些工具构建测试,而不必在系统上运行这些系统命令


有没有办法模拟系统的响应

如果您的代码运行大量外部命令,您应该创建一个简单的包装器并模拟:

def wants_to_run_external_command(command_runner)
  command_runner.run('echo "hello world"')
end
然后像这样测试它:

it 'echos "hello world"' do
  command_runner = double()
  command_runner.should_receive(:run).with('echo "hello world"')
  subject.wants_to_run_external_command(command_runner)
end

如果希望更整洁,可以使用命令运行器实例初始化对象,这样当然就不必将其传递给方法。

为这些系统命令编写包装器怎么样?然后嘲笑他们的抱怨。这是一个退路,但我发现Ruby有9次有一些简单的魔法命令可以解决我所有的问题。我希望这不是第一次