Joey Geiger
2008-Oct-10 15:29 UTC
[rspec-users] 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.
Pat Maddox
2008-Oct-10 16:06 UTC
[rspec-users] Mocking Kernel.system or Kernel.` for external application calls
"Joey Geiger" <jgeiger at gmail.com> writes:> 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. > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-usersHi 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
Pat Maddox
2008-Oct-10 16:07 UTC
[rspec-users] Mocking Kernel.system or Kernel.` for external application calls
> 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
Joey Geiger
2008-Oct-10 17:54 UTC
[rspec-users] Mocking Kernel.system or Kernel.` for external application calls
Thank you, it''s now working as expected. I was basing the code on what I had seen before, with examples of people stubbing Kernel.system, so I wasn''t thinking about just using the object itself.