Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Do you use "protected" methods to ease your own implementation, or do you document them for others to do their own subclassing? Generally, I consider a "protected" method to be part of the defined interface and therefore worth testing at the point at which it's documented and if you don't document, you should just make them private (whether with the leading "_" convention or actual checks -- that's up to you).

What I often do to test protected methods is to create a subclass that implementes a public wrapper around the protected method:

package Sub::Class; use base 'Super::Class'; sub public_setCreditAmount { shift->setCreditAmount(@_) }

Then I test that calling the protected method fails on an instance of the superclass, but that calling the public wrapper succeeds on an instance of the subclass.

That's functionally the same -- and perhaps even a bit more work -- than just setting a package and @ISA for a limited scope in the test script itself:

# in the test script { package Sub::Class; our @ISA = 'Super::Class'; my $got; eval { $got = $obj->setCreditAmount( @argument ) }; is( $@, q{}, "setCreditAmount called as subclass" ); is( $got, $expected, "setCreditAmount return value correct" ); }

However, since I usually wind up testing other properties of a subclass, too, I tend to just write the subclass object so I can keep adding to it as I need it.

-xdg

Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.


In reply to Re: How deep should unit tests go? by xdg
in thread How deep should unit tests go? by gargle

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2024-03-28 07:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found