perlmockingrt

Mocking a REST:Client::REST


I'm trying to do unit testing over an RT system, so I need to mock a RT instance locally. Basically, I'm connecting to the RT system and I'm working over the ticket's queue. Does anybody has like a code example or any ideas? I think I need to mock LWP::UserAgent but I'm not sure. Please ideas. Thanks in advance!


Solution

  • Find below my example:

    my $mock = Test::MockModule->new('REST::Client');
    my $raw_response = '';
    $mock->mock(
        POST => sub { # You can do the same for GET :)
            my ($ua, $request) = @_;
            if ($request =~ /confirm/) {
                $raw_response = $confirm_response_ok; # This is response for Confirm Method in my Code
            }
            elsif ($request =~ /transfers/) {
                $raw_response = $create_response_ok; # This is response for Create transfer in my Code
            }
            return '';
        },      
        responseCode => sub {
            my $self = shift;
            return '200';
        },      
        responseContent => sub {
            my $self = shift;
            return $raw_response;
        }
    );
    

    From Miami with Love. You know where to find us :)

    Evelio and Harold