NAME
Test::DBIx::Class::Factory - Automatically create test data for DBIx::Class
SYNOPSIS
use Test::DBIx::Class::Factory;
# Assuming you use Test::DBIx::Class to create your test data schema
use Test::DBIx::Class {
schema_class => 'Test::DBIx::Class::Example::Schema',
};
my $schema = Schema;
my $factory = Test::DBIx::Class::Factory->new( schema => $schema );
my $person = $factory->create_record( 'Person', name => 'Gareth Harper' );
is($person->name,'Gareth Harper','Record was created correctly with appropriate name');
# This *should* fail unless we get extremely lucky with random data
my $person = $factory->create_record( 'Person' );
is($person->name,'Gareth Harper','Record was created correctly with appropriate name');
# If you create a record which has parents they will automatically be created with random data
my $employee = $factory->create_record(
'Company::Employee',
employee => {
person => {
name => "Gareth Harper",
}
}
);
is($employee->employee->person->name,'Gareth Harper','Record was created correctly with appropriate name');
# If you have an existing object you would like to use in the heiarchy
my $person = $factory->create_record( 'Person', name => 'Gareth Harper' );
my $employee = $factory->create_record(
'Company::Employee',
employee => {
person => $person,
}
);
is($employee->employee->person->name,'Gareth Harper','Record was created correctly with appropriate person');
DESCRIPTION
The goal of this distribution is to make creation of test data for DBIx::Class based applications/libraries much simpler, so your test cases can focus on the actual tests rather than creating the test data that they run on. It does this by allowing you to create objects in the database and automatically creating the object heiarchy (any parent objects required) for you. It will fill any unspecified columns with randomised data so that you do not rely on it in your tests and they will/should break if that data is relied upon.
METHODS
new
This instantiates a new factory object, you need to pass in a DBIx::Class::Schema object for it to perform its work on.
my $factory = Test::DBIx::Class::Factory->new( schema => $schema );
create_record
This is the main method for this factory. This creates you a DBIx::Class::Row object of your specified type and will automatically create any required parent objects for you.
$factory->create_record( 'Person' );
If you want any specific data in the record you can pass those in as secondary arguments
$factory->create_record( 'Person', name => 'Gareth Harper' );
If you want specific data in a parent record for this entry you can do so by specifying it in the parent relationship name for that record.
$factory->create_record( 'Person::Employee', person => { name => 'Gareth Harper' } );
This also works at arbitrary nested levels.
$factory->create_record( 'Company::Employee',
employee => {
person => { name => 'Gareth Harper' },
}
);
If you have an already created object of the appropriate type you would like to use instead you can also use that.