the_ccalderon
the_ccalderon

Reputation: 2084

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!

Upvotes: 1

Views: 354

Answers (1)

Harold
Harold

Reputation: 94

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

Upvotes: 2

Related Questions