0

目前,我正在构建有以下两行,我想为其创建单元测试:

system @command.join(' ')
exit $?.exitstatus

现在我知道我可以做这样的事情:

Kernel.should_receive(:system).with()
Kernel.should_receive(:exit).with(0)

但是,当 gem 调用时,$?.exitstatus我无法模拟/存根。

有谁知道如何做到这一点??

4

1 回答 1

1

基于

你不应该存根Kernel,你应该system从当前类存根。

例如

#user.rb
def self.my_test
  system('ls')
end

#test
User.should_receive(:system).and_return('aaa')
User.my_test # => 'aaa'

any_instance如果不在类范围内调用,不要忘记使用存根

于 2013-09-19T13:55:35.740 回答