In some cases doing expect(Kernel).to receive(:system)
is not enough.
Consider this example:
foo_component.rb
class FooComponent
def run
system('....')
end
end
foo_component_spec.rb
require 'spec_helper'
describe FooComponent do
let(:foo_component) { described_class.new }
describe '#run' do
it 'does some awesome things' do
expect(Kernel).to receive(:system).with('....')
foo_component.run
end
end
end
It will not work. This is because Kernel
is a module and Object
(parent class) is mixes in the Kernel
module, making all Kernel
method available in "global" scope.
This is why proper tests should looks like this:
require 'spec_helper'
describe FooComponent do
let(:foo_component) { described_class.new }
describe '#run' do
it 'does some awesome things' do
expect(foo_component).to receive(:system).with('....')
foo_component.run
end
end
end