http://qs321.pair.com?node_id=515838


in reply to How to mock results to repeated $sth->execute() calls (DBD::Mock::Session)

This is a slightly thorny problem—when I was working on some tests with DBD::Mock, I tried to come up with a good general solution for this issue and the possibility of failure during a prepare call, and ran out of time to do it. I did come up with an adequate hack for this part, though:

@@ -1081,7 +1087,11 @@ # print STDERR "Adding Results: " . (join " | " => map { join ", " + => @{$_} } @{$current_state->{results}}) . "\n"; # copy the result sets so that # we can re-use the session - $dbh->STORE('mock_add_resultset' => [ @{$current_state->{results} +} ]); + if (my @results = @{$current_state->{results}} ) { + $dbh->STORE('mock_add_resultset' => [ @results ]); + } else { + $self->{state_index}++; + } } sub verify_bound_params {

To use it, you'd put an entry in the session looking like {statement => "select ...", results => [] } at the place you expected the prepare to happen, then normal entries for the execute calls.

Hope this helps!



If God had meant us to fly, he would *never* have given us the railroads.
    --Michael Flanders

Replies are listed 'Best First'.
Re^2: How to mock results to repeated $sth->execute() calls (DBD::Mock::Session)
by tirwhan (Abbot) on Dec 11, 2005 at 16:27 UTC

    Thanks a lot ChemBoy, that put me on the right track. I still had to modify execute() to put the correct result set into the statement tracker, but now it seems to work fine.

    Here's the change I made, for posterity (it's a godawful hack, but maybe it'll still be useful to someone at some point):

    @@ -559,6 +559,10 @@ my $dbh = $sth->{Database}; eval { $session->verify_bound_params($dbh, $tracker->bound_param +s()); + my $idx=$session->{state_index}-1; + my @results=@{$session->{states}[$idx]{results}}; + shift @results; + $tracker->{return_data}=\@results; }; if ($@) { my $session_error = $@;

    Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -- Brian W. Kernighan