title: How can I mock a sub in another module?
body: <p>I am having troubles mocking a subroutine in another module than where I am running the tests.</p>
  
  <p>I have my tests in a file called ParserTests.pl. I am trying to test a subroutine (parse) in a module LogParser.pm</p>
  
  <pre><code>sub parse {
      my ($self) = @_;
      my $rr = $self-&gt;getRR;
      while(1) {
          my $result = $self-&gt;parseCommitSet();
          if ($result eq 2) {
              last;
          }
          my $printStatus = $self-&gt;printOut($result);
          if (!$printStatus) {
              say "Problem occurred with writing to output";
              return 0;
          }
          $self-&gt;setRR(ReportRecord-&gt;new());
      }
      return 1;
  }
  </code></pre>
  
  <p>I am trying to mock printOut so that it always returns true. What I am trying to do is this:</p>
  
  <pre><code>#! /usr/bin/perl
  use v5.10.0;
  use strict;
  use warnings;
  use Test::More 'no_plan';
  use Test::MockObject;
  use LogParser;
  {other tests…}
  my $mock = Test::MockObject-&gt;new();
  $mock-&gt;set_true('LogParser::printOut');
  my $test100FH = getTestFH($test100SetsNoPrev);
  $logParser = LogParser-&gt;new($test100FH);
  is($logParser-&gt;parse, 1, "im ok?");
  close $test100FH;
  </code></pre>
  
  <p>But this test is failing. Can you tell me why and point me in the right path to get it working correctly for when I test parse()? I read up on a bunch of documentation but something like this is still a bit unclear.</p>
  
  <p>The error is </p>
  
  <pre><code>Can't use an undefined value as a symbol reference at /Users/achu/Documents/workspace/Perl_Script/LogParser.pm line 241, &lt;$fh&gt; line 8371.
  # Looks like your test exited with 25 just after 91.
  </code></pre>
  
  <p>That line (line 241) is inside the printOut subroutine though which means that it's not mocking that subroutine like I wanted it to. What am I doing wrong?</p>