2

I have a factory class which provides a bunch of similar methods by using autoload. For a longer list of different object types it can do things like

Factory->objects();
Factory->object(23);
Factory->object(name => "foo");

Now I want to write a test for this class. I started with something like this:

use Test::More;

BEGIN { use_ok 'Factory' }

my $objects = Factory->objects;

# more tests following ...

The test fails in the line with Factory->objects because it implicitly checks if Factory can do objects(). I could not find some documentation for this. But if I run the same call in a non-testing script it works perfectly.

How to test this?

Update: ARGH, I've just recognized I put all of this in the test for the Factory model class in my Catalyst app. Well, this model class is an Adapter for the Factory class in my external model (which I actually intended to test). The work perfectly for the model itself. Still would appreciate to know how to test method from an adapted class. This is how the adapter class looks like:

package MyCatalystApp::Model::Factory;
use Moose;

extends 'Catalyst::Model';
extends 'Catalyst::Model::Adaptor';

__PACKAGE__->config(class => 'MyModel::Factory');

MyModel::Factory is the same class as Factory in the original question. I skipped the difference between Catalyst and the model in the original question for simplification.

Daniel Böhmer
  • 14,463
  • 5
  • 36
  • 46

2 Answers2

0

Catalyst instantiates models (components) during setup_components by calling the COMPONENT method. I guess Catalyst::Model::Adaptor relies on this happening.

When you use MyCatalystApp::Model::Factory you can get away with calling my $factory = MyCatalystApp::Model::Factory->COMPONENT() from within tests instead of new() to make them work.

0

You should simply add "use Factory;" before calling the tests (after use_ok).

DVK
  • 126,886
  • 32
  • 213
  • 327
  • As a note - I'm pretty sure it will work, but based on my limited but non-trivial knowledge of Test::More, I can't quite figure out WHY would use_ok not play well with AutoLoader. I'm hoping someone with more clue (Schwern?) can add a more definitive answer – DVK Nov 22 '11 at 13:46
  • I tested it and it didn't work for me. ATM I am trying to build some short example code to reproduce the problem. – Daniel Böhmer Nov 22 '11 at 14:13
  • Now the tests work and your advice really makes no difference at all. – Daniel Böhmer Nov 22 '11 at 14:21
  • @halo - now THAT makes a lot more sense (as I noted in the first comment) – DVK Nov 22 '11 at 15:11