I started out using an in-memory driver for CHI and pre-warmed that with a few requests and this worked but liked the idea of injecting at the User Agent level. After a little wrangling with the Mojo::UserAgent classes which are a little new to me I ended up working out how to inject the HTTP response with a little help from Sub::OverRide . Essentially in my test scripts I can now just inject a response like so:
my $override3 = Sub::Override->new('Mojo::Transaction::res', sub { my $res2 = Mojo::Message::Response->new ; $res2->code( 200 ); $res2->body( "TESTED FINE" ); return $res2; });

I also came across Mojo::UserAgent::Mockable but it took quite a while to install and seemed to introduce a lot of new dependencies so I put that on the backburner - may be worth a look at again in the future.

It also allows you to continue with new overrides as you progress but one little thing that caught me out was that each override needs to be assigned to a distinct variable and you cannot re-use the same one. I expect that this could be avoided by blocking each instance as described in the docs but the approach I'm using now serves my needs well.

As I continue to work at improving my test writing I'll def. use this injection approach or a refinement of it going forward.


In reply to Re: OK to Include CHI File Data in module Tests ? by localshop
in thread OK to Include CHI File Data in module Tests ? by localshop

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.