0

Currently a I am build has the following two lines which I want to create a unit test for:

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

Now I know I can do something like this:

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

However when gem calls $?.exitstatus I've been unable to mock/stub this.

Does anyone know how to do this??

Jonathan Warykowski
  • 378
  • 1
  • 4
  • 14

1 Answers1

1

based on this

You should stub not Kernel, you should stub system from the current class.

for example

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

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

Do not forget to use stub for any_instance if it is called not in class scope

Community
  • 1
  • 1
gotva
  • 5,919
  • 2
  • 25
  • 35
  • Hi, Gave this ago and now it works as intended with the following, in my case anyhow: `Cukesparse.should_receive(:system).with('test')` `Cukesparse.should_receive(:exit)` Cheers, Jon – Jonathan Warykowski Sep 19 '13 at 18:00