in reply to Test::MockDBI example [SOLVED]

If you want to mock your DbLibTry class instead of DBI, as you also seem to want to do, you could look into something like Test::MockObject (which Test::MockDBI happens to use).

Also, note that you're not really doing anything with your $mock_dbi object. You'd probably need a way to inject it into your DbLibTry object for it to take effect (like maybe $dblibtry->{dbh} = $mock_dbi;?). (Untested, and the Test::MockDBI documentation is a little lacking so I'm not completely sure.)

Replies are listed 'Best First'.
Re^2: Test::MockDBI example
by brilant_blue (Beadle) on Feb 09, 2015 at 18:26 UTC
    documentation is a little lacking so I'm not completely sure

    Yes, there is a big lack of documentation (especially for beginners in db testing and testing in general). How to test code like a professional when there are almost no tutorials?

Re^2: Test::MockDBI example
by brilant_blue (Beadle) on Feb 10, 2015 at 04:24 UTC

    Why should I want to mock my DbLibTry class? I just want to mock all connections to a database.

      Why should I want to mock my DbLibTry class?

      Mocking DbLibTry::get_all_songs() is what you seem to be asking for when you write $mock_dbi->set_retval( method => 'get_all_songs', ...

      I just want to mock all connections to a database.

      Then you should be replacing your real DBI object with the fake one, as suggested earlier, something like $dblibtry->{dbh} = $mock_dbi; - and in this case of course you shouldn't call your connect, or you'll get a real DBI object.

        Yes, you are right with the replacement of real DBI object with the fake one.

        I found the Perl Practical Test-Driven Development Presentation on the Internet where it is nicely described. (Thank you Google!) The presentation is about TDD but it covers also database tests. It can be freely downloaded from here:

        http://cdn.oreillystatic.com/en/assets/1/event/12/Practical Test-driven Development Presentation.pdf

        Well, I think my problem is solved. When I'll have next problem I'll let you know about it :-)

        Thank you.