ruby - 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
  command_runner = double()
  command_runner.should_receive(:run).with('echo "hello world"')
  subject.wants_to_run_external_command(command_runner)
end

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

关于ruby - Rspec 与系统交互,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5583227/

相关文章:

Ruby Cyphering 导致非字母数字字符

mysql - 使用gem安装mysql时出现错误 "Bundler cannot continue..."

PHPUnit 测试位于特征中的静态函数

linux - 在 linux 命令历史记录中登录主机名/IP

ruby - 在井字游戏中调试递归 MiniMax

python - 从服务器端模型生成无状态客户端表单?

ruby-on-rails - FactoryGirl 在数据库中找不到实例

visual-studio-2008 - 在 VSTestHost 下运行 WatiN 测试的线程问题

operating-system - 在没有虚拟内存支持的系统中链接和分页

fork 期间写时复制