You only need to mock the members of the SOAP::Lite interface that you are actually using. Consider:

use strict; use warnings; use Test::MockObject; use Test::More; my $mockSOAP = Test::MockObject->new(); $mockSOAP->fake_module('SOAP::Lite', new => sub {SOAP_new ($mockSOAP, +@_)},); $mockSOAP->mock(soapversion => \&SOAP_soapversion); use_ok('SOAP::Lite'); my $client = SOAP::Lite->new(proxy => 'blah'); is($client->soapversion(), 1.2, 'Version ok'); eval {$client->soapversion(1.3)}; my $result = $@; is($result, "Can't set SOAP version to 1.3\n", 'Bad version rejected') +; done_testing(); sub SOAP_new { my ($mock, $class, %params) = @_; $params{version} ||= 1.2; $mock->{newParams} = \%params; $mock->{newClass} = $class; return $mock; } sub SOAP_soapversion { my ($self, $version) = @_; die "Can't set SOAP version to $version\n" if defined $version && $version != 1.2 && $version != 1.1; $self->{newParams}{version} = $version if defined $version; return $self->{newParams}{version}; }

Prints:

ok 1 - use SOAP::Lite; ok 2 - Version ok ok 3 - Bad version rejected 1..3

True laziness is hard work

In reply to Re: Unit tests with MockObjects by GrandFather
in thread Unit tests with MockObjects by rovingeyes

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.