开发者

How much do I need to test Moose- and MooseX::FollowPBP-generated methods?

I want to start strictly doing Test-Driven-Development. However, I was wondering how much I sh开发者_高级运维ould test methods generated by Moose and MooseX::FollowPBP. For example, I have the following class:

package Neu::Series;
use Moose;
use MooseX::FollowPBP;

use File::Find::Wanted;

has 'file_regex' => (
    isa=>'RegexpRef',
    is=>'rw',
    default => sub{
                 qr{
                   [A-Z]       #Uppercase letter
                   [a-zA-Z]*   #any letter, any number of times
                   [-]         #dash
                   (           #open capturing parenthesis
                   [0-9]
                   [0-9]
                   [0-9]
                   [0-9]
                   [a-zA-Z]?   #any letter, optional
                   )           #close capturing parenthesis
               }xms;
           },
);


has 'top_dir' => (
    isa=>'Str',
    is=>'rw',
);


has 'access' =>(
    isa=>'Neu::Access',
    is=>'ro',
    required=>1,

);

1;

My current test script is:

use strict;
use warnings;
use Test::More tests => 8;
use Neu::Access;

BEGIN{ use_ok('Neu::Series'); }

can_ok( 'Neu::Series', 'new');
can_ok( 'Neu::Series', 'set_file_regex');
can_ok( 'Neu::Series', 'get_file_regex');
can_ok( 'Neu::Series', 'set_top_dir');
can_ok( 'Neu::Series', 'get_top_dir');
can_ok( 'Neu::Series', 'get_access');

my $access = Neu::Access->new(dsn => 'test');
my $series_worker = Neu::Series->new(access => $access);

isa_ok($series_worker, 'Neu::Series');

Is this enough or too-much testing? (That is, besides the obviously missing tests for the regex).

I thought I saw a web page or another post about this somewhere, but I haven't been able to find it today.


There's really no point in testing that the accessors were generated correctly. If they're not, you'll find out very quickly, because any real tests you write will fail.

Moose itself tests that accessors are generated correctly, that Moose-using classes get a constructor, and so on. One of the points of using dependencies is so that you can focus on writing and testing your application, not helper code.

I do agree with daotoad, it's probably worth testing constraints and coercions that you write yourself.


Checking that all accessors were generated correctly is fine... however there are other things you could test at a slight higher level, e.g. why not test that the attributes were generated properly?

use Test::Deep;
my @attrs = Neu::Series->meta->get_all_attributes;
cmp_deeply( [ map { $_->name } @attrs ], superbagof(qw(file_regex top_dir access)));


I'd focus on testing my specification. Did I tell Moose what I wanted it to do correctly?

To this end, I'd start with the following tests:

  • Verify that read/write attributes have both an accessor and a mutator.
  • Verify that read only attributes have an accessor and no mutator.
  • Test any type constraints and coercions. Verify that only acceptable values can be set. If an attribute sIf you expect VII to be seen as a Str and coerced into an Int as 7, test that it does.


Thank you Dave, daotoad, Ether, Elliot, and brian. From reading your answers, comments, and blogs, there seem to be two salient points:

(1) No testing is needed to make sure Moose does what it is supposed to do. I think everyone agrees on this point.

(2) Testing Moose-generated methods is appropriate for establishing, testing, and maintaining your interface. Most agree on this point.

Again, thanks to everyone for your input.

EDIT:

This is just a community-wiki summary. Please read the individual answers and comments.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜