【发布时间】:2014-02-08 11:31:16
【问题描述】:
我有一些代码可以对 Linux 操作系统进行 shellout 调用,它将运行特定于发行版的命令。我试图确保测试可以在任何系统上运行,所以我对Mixlib::ShellOut 调用使用了测试替身。这是复制我的问题的简化版本:
require 'mixlib/shellout'
class SelinuxCommand
def run
runner = Mixlib::ShellOut.new('getenforce')
runner.run_command
end
end
我的测试存根Mixlib:ShellOut.new 返回一个测试替身,然后说:run_command 应该返回字符串'Enforcing':
require 'rspec'
require_relative 'selinuxcommand'
describe SelinuxCommand do
it 'gets the Selinux enforcing level' do
command = SelinuxCommand.new
Mixlib::ShellOut.stub(:new).and_return(double)
allow(double).to receive(:run_command).and_return('Enforcing')
expect command.run.to eql 'Enforcing'
end
end
但是,当我运行测试时,我看到:
$ rspec -fd selinuxcommand_spec.rb
SelinuxCommand gets the Selinux enforcing level (FAILED - 1)
Failures:
1) SelinuxCommand gets the Selinux enforcing level
Failure/Error: expect command.run.to eql 'Enforcing'
Double received unexpected message :run_command with (no args)
# ./selinuxcommand.rb:5:in `run'
# ./selinuxcommand_spec.rb:9:in `block (2 levels) in <top (required)>'
Finished in 0.00197 seconds 1 example, 1 failure
Failed examples:
rspec ./selinuxcommand_spec.rb:5 # SelinuxCommand gets the Selinux enforcing level
我不明白为什么当我明确地将它设置为期望时,双重不期望:run_command。我错过了什么?
【问题讨论】: