[rspec-users] Mocking Kernel.system or Kernel.` for external application calls

Joey Geiger jgeiger at gmail.com
Fri Oct 10 11:29:58 EDT 2008


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.


More information about the rspec-users mailing list