Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DBIx:Class - cannot find source for model

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:

enter image description here

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.

like image 954
Luke Avatar asked Oct 11 '14 15:10

Luke


1 Answers

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.

like image 120
Ashley Avatar answered Sep 20 '22 22:09

Ashley