kraih/mojo — Issue #294: need easy way to mock a request
Author: xenoterracide
Created: 2012-02-24 20:20:45+00:00
State: open

I currently have the following very evil, very dirty way of mocking a request object.

package TestModel {
use parent 'Exporter';
use JSON;

our @EXPORT = ( qw( TestRequest ) );

sub TestRequest {
my ( $model, $entity, $id ) = @_;

my %ids = ( id => $id );

my $req = Mojo::Message::Request->new;
my $url = Mojo::URL->new;

my $string_url = "http://localhost/$model";
$string_url .= "/$id" if $id;

$url->parse($string_url);

$req->method('GET');
$req->url( $url );
$req->body( to_json( $entity ) );

my $params = Mojo::Parameters->new( %ids );

$req->{body_params} = $params; # hack to inject object for testing

return $req;
}
}

I need this due to the fact that I made my model's object constructor take a request object, but I then want to test the model. I't be nice if there were some easier way of instantiating a request object, for the purpose of using it in tests, specifically with named parameters in mind.