I am trying to use DBIx:Class. I have successfully created the Schema class using DBIx:class::Schema::Loader.
I can also connect to the database.
#!/usr/bin/perl -w
use Test::More tests => 5;
use_ok('Models::ModelRole');
use_ok('Models::User');
my $model = Models::User->new();
cmp_ok($model->{ModelName}, 'eq', 'User', 'model name');
ok($model->connect(), "connect"); #works
ok($model->{schema}->resultset('User'));
The last test returns the error message:
DBIx::Class::Schema::source(): Can't find source for User at ./tests/ModelsTests.pl line 29
This is the structure of the generated class from DBIx:Class::Schema::Loader:
This is the model user class:
package Models::User;
use DB::Glued::Schema::Result::User;
use Models::ModelRole;
use Moose;
with 'Models::ModelRole';
sub BUILD {
my $self = shift;
$self->{schema} = Glued::Schema::Result::User->new();
my @name = split('::', __PACKAGE__);
$self->{ModelName} = $name[-1];
}
1;
I hope this is enough information.
Schemata/models have to be connected to a source. The DBIC code is only describing the data and its relationships. It's entirely agnostic about the source/connection.
So you must connect DB::Glued::Schema
to be able to exercise the model. The best way for tests, I think, is to connect to an in :memory:
SQLite DB. The DB will be empty of course. There are a few options/approaches for populating it if you need fixtures. Search metacpan if you do.
There is a nice package to make test connections simple for you: Test::DBIx::Class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With