Forum: RSpec Mocking Kernel.system or Kernel.` for external application calls

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
0e7d5331c8315f1cdba91506ed360195?d=identicon&s=25 Joey Geiger (jgeiger)
on 2008-10-10 17:30
(Received via mailing list)
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.
42172acdf3c6046f84d644cb0b94642c?d=identicon&s=25 Pat Maddox (pergesu)
on 2008-10-10 18:14
(Received via mailing list)
"Joey Geiger" <jgeiger@gmail.com> 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
> rspec-users@rubyforge.org
> 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
42172acdf3c6046f84d644cb0b94642c?d=identicon&s=25 Pat Maddox (pergesu)
on 2008-10-10 18:20
(Received via mailing list)
> Kernel.should_receive(:system).with(/omssa_aws2ez2_unix\.pl/).and_return(true)
> is what you're looking for.

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

Pat
This topic is locked and can not be replied to.