title: Perl Unit Test Mocking
body: <p>I am attempting write a unit test in Perl by mocking. Its my first attempt at mocking.</p>
  
  <p>Method is defined in MyConnect.pm</p>
  
  <pre><code>sub connect_attempt {
  my ($a, $b, $c) = @_; 
  // some calculation with $c
  $status = connect( $a, $b );
  return status;
  }
  </code></pre>
  
  <p>connect is subroutine defined in Socket module. I need to test two scenario </p>
  
  <p>1) Connection successful  - To test this I am planning to return a new mock object or string<br>
  2) Connection unsuccessful - Status will remain undefined</p>
  
  <p>MyConnect.t</p>
  
  <pre><code>use MyConnect;
  use Socket;
  my $status;
  my $mock = Test::MockObject-&gt;new();
  my $connect_module = Test::MockModule-&gt;new('MyConnect');
  $connect_module-&gt;mock( 'connect_attempt' =&gt; sub { return "true"});
  my $socket_module = Test::MockModule-&gt;new('Socket');
  $socket_module-&gt;mock( 'connect' =&gt; sub { return "true"});
  $status = $connect_module-&gt;connect_attempt("122", "53", 0);
  ok(defined $status, 'Its fine');
  </code></pre>
  
  <p>I get the below error:</p>
  
  <pre><code>Cant locate object method "connect_attempt" via package "Test::MockModule" at t/MyConnect.t line 21.
  #Looks like your test exited with 9 before it could output anything.
  Dubious, test returned 9 (wstat 2304, 0x900)`
  Line 21 is the call `$status = connect_attempt("122", "53", 0);
  </code></pre>
  
  <p>As per my understanding, I have created MockModule for my Module(MyConnect) and Socket module since I will invoking connect subroutine from it. 
  I have set the response from both subroutines to be true. I should be expecting $status to true as well.</p>
  
  <p>Can someone please explain where I am going wrong. This is my first unit test with mocking. I may have misunderstood certain concept.</p>
  
  <p>Thanks</p>