Fix problems with Moose style object creation.

This commit is contained in:
Colin Kuskie 2010-07-07 12:31:37 -07:00
parent e5a2af0330
commit 416f62f36a
2 changed files with 23 additions and 3 deletions

View file

@ -50,12 +50,12 @@ around BUILDARGS => sub {
##Need same db code as below here.
##Session check goes here?
##Build a new one
my $session = $_[0]->{session};
my $properties = $_[0];
my $session = $properties->{session};
if (! (blessed $session && $session->isa('WebGUI::Session')) ) {
WebGUI::Error::InvalidObject->throw(expected=>"WebGUI::Session", got=>(ref $session), error=>"Need a session.");
}
my ($vendorId, $dateCreated) = $class->_init($session);
my $properties = {};
$properties->{vendorId} = $vendorId;
$properties->{dateCreated} = $dateCreated;
return $class->$orig($properties);

View file

@ -31,7 +31,7 @@ my $session = WebGUI::Test->session;
#----------------------------------------------------------------------------
# Tests
plan tests => 51;
plan tests => 55;
#----------------------------------------------------------------------------
# put your tests here
@ -67,6 +67,19 @@ cmp_deeply(
'new: requires a session variable',
);
eval { $vendor = WebGUI::Shop::Vendor->new({ userId => 3, }); };
$e = Exception::Class->caught();
isa_ok($e, 'WebGUI::Error::InvalidObject', 'new via property hash takes an exception to not giving it a session variable');
cmp_deeply(
$e,
methods(
error => 'Need a session.',
got => '',
expected => 'WebGUI::Session',
),
'... requires a session variable',
);
eval { $vendor = WebGUI::Shop::Vendor->new($session); };
$e = Exception::Class->caught();
isa_ok($e, 'WebGUI::Error::InvalidParam', 'new takes an exception to not giving it a vendor id to instanciate');
@ -90,6 +103,13 @@ cmp_deeply(
'new: requires a valid vendorId',
);
my $test_vendor = eval { WebGUI::Shop::Vendor->new({ session => $session, }); };
$e = Exception::Class->caught();
ok(!$e, 'new via property hash with session');
isa_ok($test_vendor, 'WebGUI::Shop::Vendor', '... returns correct type of object');
WebGUI::Test->addToCleanup($test_vendor);
$test_vendor->delete;
eval { $vendor = WebGUI::Shop::Vendor->new($session, 'defaultvendor000000000'); };
$e = Exception::Class->caught();
ok(!$e, 'No exception thrown');