Mocking Kernel.system or Kernel.` for external application calls

I’m running into an issue while trying to test the execution of some
external applications called from inside my models.

class OmssaPacker
def self.run_omssa_aws2ez2_unix(parameters)
system(“perl /pipeline/vipdac/lib/omssa_aws2ez2_unix.pl
#{parameters}”)
end
end

…spec code…
describe “run omssa aws2ez2 unix” do
it “should run the perl file” do
Kernel.should_receive(:system).with(/omssa_aws2ez2_unix.pl/).and_return(true)
OmssaPacker.run_omssa_aws2ez2_unix(“file”).should be_true
end
end
…spec code…

Returns:
should run the perl file
Mock ‘Module’ expected :system with (/omssa_aws2ez2_unix.pl/) once,
but received it 0 times

And it also actually attempts to run the perl script, since I can see
the output in the window. If I change the code to:

def self.run_omssa_aws2ez2_unix(parameters)
%x{ perl /pipeline/vipdac/lib/omssa_aws2ez2_unix.pl #{parameters} }
end

…spec code…
describe “run omssa aws2ez2 unix” do
it “should run the perl file” do
Kernel.should_receive(:`).with(/omssa_aws2ez2_unix.pl/).and_return(true)
OmssaPacker.run_omssa_aws2ez2_unix(“file”).should be_true
end
end
…spec code…

Returns:
should run the perl file
expected true, got "“Program: omssa_aws2ez2.pl …output from perl
program about command line options…”

It seems that the attempts to mock Kernel are being ignored and it’s
just running the scripts anyway. I’m guessing that I’m setting up the
mock incorrectly, but I’m not sure how.

I’ve also tried Kernel.stub!(:system).and_return(true) with the same
results.

“Joey G.” [email protected] writes:

describe “run omssa aws2ez2 unix” do
but received it 0 times
it “should run the perl file” do

It seems that the attempts to mock Kernel are being ignored and it’s
just running the scripts anyway. I’m guessing that I’m setting up the
mock incorrectly, but I’m not sure how.

I’ve also tried Kernel.stub!(:system).and_return(true) with the same results.


rspec-users mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/rspec-users

Hi Joey,

In Ruby, any time you send a message without an explicit receiver, the
message has an implicit receiver of ‘self’. So from this we can infer
that you don’t want to stub Kernel#system, but rather
OmssaPacker.system. The other side of the coin for understanding this
is that while the method is defined in Kernel, it’s not defined ON
Kernel. Kernel is a module that’s mixed into Object, so its methods are
defined on all Objects.

Kernel.should_receive(:system).with(/omssa_aws2ez2_unix.pl/).and_return(true)
is what you’re looking for.

Pat

Kernel.should_receive(:system).with(/omssa_aws2ez2_unix.pl/).and_return(true)
is what you’re looking for.

erm, that’s a tad embarrasing :slight_smile: I copy/pasted your line without
editing it! It should be:
OmssaPacker.should_receive(:system).with(/omssa_aws2ez2_unix.pl/).and_return(true)

Pat