A Little Moose Help
Well I was busily writing some tests for a large processor interface where you would pass an object too it and it will merrily process away on it. Well all of the object I was passing into it are written in Moose and I did not need to test the 'State' or 'values' of these object, what I needed to tests where some of the lower-level admin functions in the interface, that really had nothing to do with the objects themselves more about reading config files connecting and alike.
The problem I was having was to test the 'Interface' I had to give it a valid 'object' as that was a 'required' attribute. That I could live with it was all the objects I was was going to pass into it needed to be instancates and they all had a large number of 'required' attributes as well.
Well it did make for a good deal of extra tying and sometimes very annoying bugs when I put in the wrong value for a field type or missed a field all together or the fields was coerced from a hash and even an array and on and on.
Well I had to create a number of dummy hashes just to start testing the processor. Needless to say I got very tired and coss-eyed from all that extra typing to say nothing of the time wasted figuring out what was the correct data and format for these objects when in the end I am not even going to use them.
Well I was almost ready to got to the offending objects and remover all the required attributes until I took a look on CPAN (I figured someone else must of had this problem before) and there is was MooseX::NotRequired
So I loaded it, removed the 150+ lines of now un-needed hash-refs and gave this a go
use Test::More tests => 16;
use MooseX::NotRequired;
my $order_types = {
new_po => MooseX::NotRequired::make_optional_subclass('Product::BRULE::PO'),
new_cod => MooseX::NotRequired::make_optional_subclass('Product::BRULE::COD),
new_standing => MooseX::NotRequired::make_optional_subclass('Product::BRULE::Standing_Offer),
...
}
foreach my $type (keys(%{$order_types})){
my $order = $order_types{$type}->new();
$order_processor->process({order=>$order,});
ok( $order_processor->count() eq 1);
...
and my tests worked like a charm, thus saving me a good deal more typing. In reality not much typing was saved as in my next batch of tests I was going to look at the BRULE objects and make sure they work correctly and there should be some data for that type of testing.
Anyway nice to have this about as it comes in handy when this sort of thing gets in our way. Time to relax now and enjoy the first 20c day of the year
Leave a comment