Greetings,

I recently got my hands dirty with Test::MockModule, and was successfully able to use it. I was just wondering if I'm heading in the right direction though, and would appreciate any comments and suggestions.

I have an object, say $obj, and I want to test its login and get_info methods. $obj uses LWP::UserAgent and both the mentioned methods make an HTTP request and expect some reponse.

For the sake of simplicity, assume these methods just return the HTTP response code. In the practice of good testing, I'm not relying on an active internet connection, and thus I decided to fake the LWP::UserAgent's request method in my test script.

This is how the test script looks like:

use Test::More tests => 2; use Test::MockModule; my $obj = MyModule->new(); # Test the login method { my $lwp = Test::MockModule->new( 'LWP::UserAgent' ); $lwp->mock( request => sub { # Return a hand crafted HTTP::Response object my $response = HTTP::Response->new; $response->code(200); $response->content('Login Successfull'); return $response; }); my $code = $obj->login(); ok($code == 200, 'login'); } # Test the get_info method { my $lwp = Test::MockModule->new( 'LWP::UserAgent' ); $lwp->mock( request => sub { # Return a hand crafted HTTP::Response object my $response = HTTP::Response->new; $response->code(302); $response->content('Some Info'); return $response; }); my $code = $obj->get_info(); ok($code == 302, 'get_info'); }

Essentially, every method of $obj involves a HTTP request, and in my test script I am mocking LWP::UserAgent's request method and returning some pre-determined data through a hand crafted HTTP::Response object.

What I need advice on is:

Thanks for reading.


--
Rohan


In reply to How to use Test::MockModule better by arc_of_descent

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.