I want to write tests for code that uses the following DBI construct:
$dbh = DBI->connect('..', '..', '..'); $sth = $dbh->prepare(q{select ..}); if ($sth && !$sth->errstr()) { while (my $row = $sth->fetchrow_arrayref()) { .. } }
Test::MockDBI sets the errstr to always return true (see line 352-354 of MockDBI.pm). That's a problem, since the code I want to test considers a true errstr to be an error condition.

That's when I thought of overriding the errstr method like how it's done in the sub fake_module of Test::MockObject:

no strict 'refs'; *{ Test::MockDBI::errstr } = sub { return };
I expect the errstr method to be overridden to return undef , but it still returns 'DBI error'. Any help or hints on how to make errstr return false is appreciated.
package db; # db.pm use strict; use warnings; use Data::Dumper; use DBI; our $dbh = undef; sub connect { $dbh = DBI->connect('DBI:mysql:test', '', '') or die "connect failed"; } sub disconnect { $dbh->disconnect(); } sub select_i_from_t { my $sth = $dbh->prepare(q{select i from t}) or die "prepare failed"; $sth->execute() or die "execute failed"; my $arref = []; if ($sth && !$sth->errstr()) { # <-- errstr is always be false in # <-- Mocked mode while (my $row = $sth->fetchrow_arrayref()) { push @$arref, $row->[0]; } } else { die "no sth" if (!$sth); die "errstr:" . $sth->errstr() if ($sth->errstr()); } return $arref; } 1; __END__ # db.t use strict; use warnings; # Signal the we want to use Test::MockDBI BEGIN { push @ARGV, "--dbitest=42"; } use Data::Dumper; use Test::More; use Test::MockDBI; BEGIN { plan tests => 4 } use_ok(q{db}); my $mock_dbi = Test::MockDBI::get_instance(); # Replace the code for sub errstr in Test::MockDBI. # Code is from Test::MockObject (CPAN) { ## no critic (ProhibitNoStrict) no warnings 'redefine'; no strict 'refs'; *{ Test::MockDBI::errstr } = sub { return }; } my $vals = [ [123], [124], [125] ]; $mock_dbi->set_retval_scalar( 42, "select i from t", sub { shift @$vals } ); ok(defined(db::connect()), q{Expect connect() to return true}); is_deeply( db::select_i_from_t(), [ 123, 124, 125 ], q{Expect select_i_from_t() to return arrayref [ 123, 124, 125 ]} ); ok(defined(db::disconnect()), q{Expect disconnect() to return true}); __END__
Running the test yields:
$ prove db.t db....Name "Test::MockDBI::errstr" used only once: possible typo at db +.t line 24. db....ok 2/4errstr:DBI error at 'select i from t' at db.pm line 32. # Looks like you planned 4 tests but only ran 2. # Looks like your test died just after 2. db....dubious Test returned status 255 (wstat 65280, 0xff00) DIED. FAILED tests 3-4 Failed 2/4 tests, 50.00% okay Failed Test Stat Wstat Total Fail Failed List of Failed ---------------------------------------------------------------------- +--------- db.t 255 65280 4 4 100.00% 3-4 Failed 1/1 test scripts, 0.00% okay. 2/4 subtests failed, 50.00% okay.
--
No matter how great and destructive your problems may seem now, remember, you've probably only seen the tip of them. [1]

In reply to How to override a mocked method with Test::MockDBI? by andreas1234567

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.