Acme-CPANModulesBundle-Import-MojoliciousAdvent-2017

 view release on metacpan or  search on metacpan

LICENSE  view on Meta::CPAN

TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGES.

                     END OF TERMS AND CONDITIONS

        Appendix: How to Apply These Terms to Your New Programs

  If you develop a new program, and you want it to be of the greatest
possible use to humanity, the best way to achieve this is to make it
free software which everyone can redistribute and change under these
terms.

  To do so, attach the following notices to the program.  It is safest to
attach them to the start of each source file to most effectively convey
the exclusion of warranty; and each file should have at least the
"copyright" line and a pointer to where the full notice is found.

    <one line to give the program's name and a brief idea of what it does.>
    Copyright (C) 19yy  <name of author>

MANIFEST  view on Meta::CPAN

Makefile.PL
README
devdata/https_mojolicious.io_blog_2017_12_01_day-1-getting-started
devdata/https_mojolicious.io_blog_2017_12_02_day-2-the-stash
devdata/https_mojolicious.io_blog_2017_12_03_day-3-using-named-routes
devdata/https_mojolicious.io_blog_2017_12_04_day-4-dont-fear-the-full-app
devdata/https_mojolicious.io_blog_2017_12_05_day-5-your-apps-built-in-commands
devdata/https_mojolicious.io_blog_2017_12_06_day-6-adding-your-own-commands
devdata/https_mojolicious.io_blog_2017_12_07_day-7-using-template-variants-for-a-beta-landing-page
devdata/https_mojolicious.io_blog_2017_12_08_day-8-mocking-a-rest-api
devdata/https_mojolicious.io_blog_2017_12_09_day-9-the-best-way-to-test
devdata/https_mojolicious.io_blog_2017_12_10_day-10-give-the-customer-what-they-want
devdata/https_mojolicious.io_blog_2017_12_11_day-11-useragent-content-generators
devdata/https_mojolicious.io_blog_2017_12_12_day-12-more-than-a-base-class
devdata/https_mojolicious.io_blog_2017_12_13_day-13-more-about-roles
devdata/https_mojolicious.io_blog_2017_12_14_day-14-you-promised-to-call
devdata/https_mojolicious.io_blog_2017_12_15_day-15-start-a-new-yancy-app
devdata/https_mojolicious.io_blog_2017_12_16_day-16-the-secret-life-of-sessions
devdata/https_mojolicious.io_blog_2017_12_17_day-17-the-wishlist-app
devdata/https_mojolicious.io_blog_2017_12_18_day-18-the-wishlist-model
devdata/https_mojolicious.io_blog_2017_12_19_day-19-make-your-app-installable

devdata/https_mojolicious.io_blog_2017_12_03_day-3-using-named-routes  view on Meta::CPAN

If a route doesn&#39;t explicitly render something, it will check to see if a template exists for the name of the route.
It is a shortcut that some people like and could have made the routing definitions look like this:</p>

<pre><code>get &#39;/toy/:toy_name&#39; =&gt; &#39;toy&#39;;
get &#39;/meet/:name&#39; =&gt; &#39;staff&#39;;
get &#39;/&#39; =&gt; &#39;home&#39;;
</code></pre>

<p>While some people appreciate that feature, I personally like to be explicit about which template I want.
I also like to separate concerns.
Anyway, you can choose whichever you like best.
After all, there&#39;s more than one way to do it!</p>

              </section>
              <small><p><a href="https://flickr.com/photos/94599716@N06/15283079263">Image</a> by <a href="https://www.flickr.com/people/94599716@N06">Travis Wise</a> licensed under the <a class="extiw" href="https://en.wikipedia.org/wiki/en:Creative...
</small>

              <p class="tags">
                <span>Tagged in </span>:
                  <a href="/blog/tag/advent/">advent</a>,
                  <a href="/blog/tag/lite/">lite</a>,

devdata/https_mojolicious.io_blog_2017_12_07_day-7-using-template-variants-for-a-beta-landing-page  view on Meta::CPAN


<pre><code>$ perl myapp.pl get /web -m beta
&lt;h1&gt;CPAN Testers&lt;/h1&gt;
&lt;p&gt;This is the main CPAN Testers application.&lt;/p&gt;
</code></pre>

<p>Template variants are a useful feature in some edge cases, and this isn&#39;t the
first time I&#39;ve found a good use for them. I&#39;ve also used them to provide a
different layout template in &quot;development&quot; mode to display a banner saying
&quot;You&#39;re on the development site&quot;. Useful for folks who are undergoing user
acceptance testing. The best part is that if the desired variant for that
specific template is not found, Mojolicious falls back to the main template. I
built a mock JSON API application which made extensive use of this fallback
feature, but that&#39;s another blog post for another time.</p>

              </section>
              <small><p><a href="https://commons.wikimedia.org/w/index.php?curid=2147460">Image</a> by Photo by and (c)2007 <a href="//commons.wikimedia.org/wiki/User:Jina_Lee" title="User:Jina Lee">Jina Lee</a> - <span class="int-own-work" lang="en"...
</small>

              <p class="tags">
                <span>Tagged in </span>:

devdata/https_mojolicious.io_blog_2017_12_08_day-8-mocking-a-rest-api  view on Meta::CPAN

                        <h5>Doug Bell</h5>
                        <p>Doug (<a href="http://preaction.me">preaction</a>) is a long time Perl user.
He is the current maintainer of <a href="http://www.cpantesters.org/">CPAN Testers</a> and the author of many <a href="https://metacpan.org/author/PREACTION">CPAN modules</a> including the <a href="http://preaction.me/statocles/">Statocles</a> blog e...

                      </div>

                  </div>

              <ul class="post-nav cf">
                  <li class="prev"><a href="/blog/2017/12/07/day-7-using-template-variants-for-a-beta-landing-page/index.html" rel="prev"><strong>Previous Article</strong> Day 7: Using Template Variants For a Beta Landing Page</a></li>
                  <li class="next"><a href="/blog/2017/12/09/day-9-the-best-way-to-test/index.html" rel="next"><strong>Next Article</strong> Day 9: The Best Way to Test </a></li>
              </ul>

            </div>

        </article>


      </div>

      <div class="four columns end" id="secondary">

devdata/https_mojolicious.io_blog_2017_12_09_day-9-the-best-way-to-test  view on Meta::CPAN

  <meta content="width=device-width, initial-scale=1, maximum-scale=1" name="viewport">

  <link href="/theme/css/default.css" rel="stylesheet">
  <link href="/theme/css/layout.css" rel="stylesheet">
  <link href="/theme/css/media-queries.css" rel="stylesheet">
  <link href="/theme/css/statocles.css" rel="stylesheet">

  <!-- twitter and opengraph -->
  <meta content="summary" name="twitter:card">
      <meta content="@joelaberger" name="twitter:creator">
  <meta content="https://mojolicious.io/blog/2017/12/09/day-9-the-best-way-to-test/" property="og:url">
  <meta content="Day 9: The Best Way to Test" property="og:title">
    <meta content="An introduction to Test::Mojo, the testing framework for Mojolicious." property="og:description">
    <meta content="https://mojolicious.io/blog/2017/12/09/day-9-the-best-way-to-test/1280px-CSIRO_ScienceImage_2798_Testing_in_the_Laboratory.jpg" property="og:image">
    <meta content="summary_large_image" name="twitter:card">

  <script src="/theme/js/modernizr.js"></script>

      <link href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/sunburst.min.css" rel="stylesheet">

  <title>Day 9: The Best Way to Test - mojolicious.io</title>
  <meta content="Joel Berger" name="author">
  <meta content="Statocles 0.093" name="generator">
  <link href="/static/favicon.ico" rel="shortcut icon">

devdata/https_mojolicious.io_blog_2017_12_09_day-9-the-best-way-to-test  view on Meta::CPAN


                  <time class="date" datetime="2017-12-09">Dec  9, 2017</time>
                  

              </p>

            </div>

              <div class="post-thumb">
                <!-- theme suggests 1300x500 -->
                <img alt="Woman using chemistry lab equipment" src="/blog/2017/12/09/day-9-the-best-way-to-test/1280px-CSIRO_ScienceImage_2798_Testing_in_the_Laboratory.jpg">
              </div>

            <div class="post-content">

              <section id="section-1">
                  <p>Ok so it is a bit of a click-bait headline.
But that doesn&#39;t mean I don&#39;t believe it.</p>

<p><a href="http://mojolicious.org/perldoc/Test/Mojo">Test::Mojo</a> is a test framework for websites and related technologies.
While its true that there are many such tools, this one gets its power comes from combining so many of the tools that Mojolicious provides.

devdata/https_mojolicious.io_blog_2017_12_10_day-10-give-the-customer-what-they-want  view on Meta::CPAN

If you have special format types you can add them to the <a href="http://mojolicious.org/perldoc/Mojolicious#types">types</a> object on the application.</p>

<h2>Responding to the Format Request</h2>

<p>There are two methods which help render what the client wants: <a href="http://mojolicious.org/perldoc/Mojolicious/Controller#respond_to"><code>respond_to</code></a> and <a href="http://mojolicious.org/perldoc/Mojolicious/Plugin/DefaultHelpers#acc...

<p>The former, <code>respond_to</code>, is much more high level and should be your go-to choice.
It takes key value pairs where the keys are the file types that should be handled (in extension format).
The values are either stash values that should be used when rendering or else a callback to be invoked.</p>

<p>Since I showed you <a href="/blog/2017/12/09/day-9-the-best-way-to-test">yesterday</a> how to use <a href="http://mojolicious.org/perldoc/Test/Mojo">Test::Mojo</a>, let&#39;s examine this as a test.
Imagine a test for an application that returns information about Santa&#39;s Reindeer.</p>

<pre><code class="hljs"><span class="hljs-keyword">use</span> <span class="hljs-function">Mojolicious::Lite</span>;

<span class="hljs-comment"># This could be a db lookup</span><span class="hljs-comment">
</span>helper &#39;<span class="hljs-string">reindeer</span>&#39; =&gt; <span class="hljs-keyword">sub </span>{
  <span class="hljs-keyword">my</span> (<span class="hljs-type">$c</span>, <span class="hljs-type">$name</span>) = <span class="hljs-type">@_</span>;
  <span class="hljs-keyword">my</span> <span class="hljs-type">%reindeer</span> = (
    rudolph =&gt; {
      name =&gt; &#39;<span class="hljs-string">Rudolph</span>&#39;,

devdata/https_mojolicious.io_blog_2017_12_10_day-10-give-the-customer-what-they-want  view on Meta::CPAN

                      <div class="about">
                        <h5>Joel Berger</h5>
                        <p>Joel has Ph.D. in Physics from the University of Illinois at Chicago.
He an avid Perl user and <a href="https://metacpan.org/author/JBERGER">author</a> and is a member of the Mojolicious Core Team.</p>

                      </div>

                  </div>

              <ul class="post-nav cf">
                  <li class="prev"><a href="/blog/2017/12/09/day-9-the-best-way-to-test/index.html" rel="prev"><strong>Previous Article</strong> Day 9: The Best Way to Test</a></li>
                  <li class="next"><a href="/blog/2017/12/11/day-11-useragent-content-generators/index.html" rel="next"><strong>Next Article</strong> Day 11: UserAgent Content Generators </a></li>
              </ul>

            </div>

        </article>


      </div>

devdata/https_mojolicious.io_blog_2017_12_11_day-11-useragent-content-generators  view on Meta::CPAN


              <div class="post-thumb">
                <!-- theme suggests 1300x500 -->
                <img alt="Artist painting" src="/blog/2017/12/11/day-11-useragent-content-generators/artist-painting-1459778857j86.jpg">
              </div>

            <div class="post-content">

              <section id="section-1">
                  <p>We have already seen <a href="http://mojolicious.org/perldoc/Mojo/UserAgent">Mojo::UserAgent</a> used to make HTTP requests in this series.
In fact we&#39;ve already seen how you can use <a href="http://mojolicious.org/perldoc/Mojolicious/Guides/Cookbook#USER-AGENT">Content Generators</a> to build requests <a href="/blog/2017/12/09/day-9-the-best-way-to-test#making-requests">in tests</a>...
But we didn&#39;t look at how they work or how you can add new ones.</p>

              </section>
              <section id="section-2">
                  <h2>Using Content Generators</h2>

<p>The UserAgent, and more specifically its <a href="http://mojolicious.org/perl/Mojo/UserAgent/Transactor">Transactor</a>, help you by making it easy to create HTTP requests.
Consider the most basic request with a body, a <code>POST</code> with a binary body, maybe ASCII text.
In that case, the request</p>

devdata/https_mojolicious.io_blog_2017_12_13_day-13-more-about-roles  view on Meta::CPAN

  $dom-&gt;find_custom-&gt;each(sub{ say });
}
</code></pre>

<h2>Roles on CPAN</h2>

<p>The first part of the ecosystem to embrace roles was testing.
Given the interface of <a href="http://mojolicious.org/perldoc/Test/Mojo">Test::Mojo</a>, adding additional test methods isn&#39;t as easy as is generally true of other testers in the <a href="https://metacpan.org/pod/Test::More">Test::More</a> lands...
Roles fill that gap nicely, and therefore such roles predated Mojo::Base&#39;s own role handling and even inspired adding it to the core.</p>

<p>As I&#39;ve mentioned <a href="/blog/2017/12/09/day-9-the-best-way-to-test">earlier in this series</a>, <a href="https://metacpan.org/pod/Test::Mojo::Role::Debug">Test::Mojo::Role::Debug</a> adds methods to run a callback on failed tests.
<a href="https://metacpan.org/pod/Test::Mojo::Role::TestDeep">Test::Mojo::Role::TestDeep</a> lets you test responses with the comparison functions from <a href="https://metacpan.org/pod/Test::Deep">Test::Deep</a>.
<a href="https://metacpan.org/pod/Test::Mojo::Role::PSGI">Test::Mojo::Role::PSGI</a> lets you use Test::Mojo with non-Mojolicious web frameworks!
You can even use them all together.</p>

<pre><code>use Mojo::Base -strict;
use Test::More;
use Test::Mojo;
use Test::Deep; # for its keywords

my $t = Test::Mojo

devdata/https_mojolicious.io_blog_2017_12_17_day-17-the-wishlist-app  view on Meta::CPAN

</code></pre>

<p><small>wishlist.pl</small></p>

<h3>Helpers</h3>

<p>I won&#39;t go into great detail today as much of the model logic will be replaced in tomorrow&#39;s article.
Still, in broad strokes, we define a persistent hash structure, the keys of which are users and the values are hashes of information.</p>

<p>Once you login, your name is stored in the <a href="http://mojolicious.org/perldoc/Mojolicious/Controller#session"><code>session</code></a> and in this hash.
While I haven&#39;t followed the <a href="https://mojolicious.io/blog/2017/12/16/day-16-the-secret-life-of-sessions/">best practices for sessions from yesterday</a>, you certainly could and should if this data mattered to you.
But also, no authentication is attempted, this is a personal wishlist app, hopefully none of your friends are going to play the Grinch on you!</p>

<p>The <code>user</code> helper is especially clever.
You can pass it a name for lookup, if that isn&#39;t provided then a name is looked for in the stash and the session in turn.
In this way you are looking up a specific user, the user being referenced by the page, or the logged in user.</p>

<p>There is also a helper that uses <a href="https://metacpan.org/pod/LinkEmbedder">LinkEmbedder</a> to look up information about a link and return it.
That is used when a user pastes a url that they want to add to their list.
LinkEmbedder will fetch that page and scrape it for metadata using several open protocols and falling back onto heuristics if possible.
It will then return the information and an short HTML representation of that resource.</p>

devdata/https_mojolicious.io_blog_2017_12_19_day-19-make-your-app-installable  view on Meta::CPAN

<pre><code>[ShareDir]
</code></pre>

<p>whose default is <code>share</code>.
I&#39;m sure other installers have ways to handle it too.</p>

<h2>File::Share Saves the Day</h2>

<p>Since we earlier moved the location of templates and static files, we now need to tell the application where we put them.
The major innovation that makes this method possible and so painless is the module <a href="https://metacpan.org/pod/File::Share">File::Share</a>.
While it isn&#39;t <a href="https://metacpan.org/pod/File::ShareDir">the original module</a> used to locate share directories after installation, it is the best one since it also works before hand during development too.
To do so it uses the heuristic of looking to see if a directory exists named <code>share</code> in that particular location (and a few checks for sanity), thus the second reason we call the directory that.</p>

<p>When used we get the absolute path to the share directory of your distribution.
Usually the name of the distribution is the name of your main module with the <code>::</code> replaced by <code>-</code>.</p>

<h2>Use in Mojolicious Apps</h2>

<p>To use File::Share in a Mojolicious (Full) app I recommend wrapping it in a <a href="http://mojolicious.org/perldoc/Mojo/File">Mojo::File</a> object and storing it in an attirbute on the app.
The attribute can be named anything, perhaps even as simple as <code>files</code>, though for the <a href="https://github.com/jberger/Wishlist/blob/blog_post/installable/lib/Wishlist.pm#L10-L14">Wishlist app</a> I have used the name <code>dist_dir</c...

devdata/https_mojolicious.io_blog_2017_12_20_day-20-practical-testing  view on Meta::CPAN

            </div>

              <div class="post-thumb">
                <!-- theme suggests 1300x500 -->
                <img alt="Taking a sample of lava at Kilauea" src="/blog/2017/12/20/day-20-practical-testing/lava.jpg">
              </div>

            <div class="post-content">

              <section id="section-1">
                  <p>Back on <a href="/blog/2017/12/09/day-9-the-best-way-to-test">Day 9</a> we discussed testing and especially <a href="http://mojolicious.org/perldoc/Test/Mojo">Test::Mojo</a>.
Today I want to just briefly talk about some practical things that can come up when testing real world applications.
Once again the discussion will be motivated by the <a href="https://github.com/jberger/Wishlist">Wishlist</a> application that we&#39;ve been developing these past few days.</p>

              </section>
              <section id="section-2">
                  <h2>Configuration Overrides</h2>

<p>In the Day 9 article I mentioned that the Test::Mojo <a href="http://mojolicious.org/perldoc/Test/Mojo#new">constructor</a> could be passed configuration overrides.
In this example we can see how that override lets us ensure that we are testing on a fresh and isolated database.</p>

devscripts/update  view on Meta::CPAN


my %daily_urls = (
    "01" => "https://mojolicious.io/blog/2017/12/01/day-1-getting-started",
    "02" => "https://mojolicious.io/blog/2017/12/02/day-2-the-stash",
    "03" => "https://mojolicious.io/blog/2017/12/03/day-3-using-named-routes",
    "04" => "https://mojolicious.io/blog/2017/12/04/day-4-dont-fear-the-full-app",
    "05" => "https://mojolicious.io/blog/2017/12/05/day-5-your-apps-built-in-commands",
    "06" => "https://mojolicious.io/blog/2017/12/06/day-6-adding-your-own-commands",
    "07" => "https://mojolicious.io/blog/2017/12/07/day-7-using-template-variants-for-a-beta-landing-page",
    "08" => "https://mojolicious.io/blog/2017/12/08/day-8-mocking-a-rest-api",
    "09" => "https://mojolicious.io/blog/2017/12/09/day-9-the-best-way-to-test",
    "10" => "https://mojolicious.io/blog/2017/12/10/day-10-give-the-customer-what-they-want",
    "11" => "https://mojolicious.io/blog/2017/12/11/day-11-useragent-content-generators",
    "12" => "https://mojolicious.io/blog/2017/12/12/day-12-more-than-a-base-class",
    "13" => "https://mojolicious.io/blog/2017/12/13/day-13-more-about-roles",
    "14" => "https://mojolicious.io/blog/2017/12/14/day-14-you-promised-to-call",
    "15" => "https://mojolicious.io/blog/2017/12/15/day-15-start-a-new-yancy-app",
    "16" => "https://mojolicious.io/blog/2017/12/16/day-16-the-secret-life-of-sessions",
    "17" => "https://mojolicious.io/blog/2017/12/17/day-17-the-wishlist-app",
    "18" => "https://mojolicious.io/blog/2017/12/18/day-18-the-wishlist-model",
    "19" => "https://mojolicious.io/blog/2017/12/19/day-19-make-your-app-installable",

lib/Acme/CPANModules/Import/MojoliciousAdvent/2017_12_09.pm  view on Meta::CPAN

package Acme::CPANModules::Import::MojoliciousAdvent::2017_12_09;

our $DATE = '2018-12-30'; # DATE
our $VERSION = '0.001'; # VERSION

our $LIST = {description=>"This list is generated by extracting module names mentioned in [https://mojolicious.io/blog/2017/12/09/day-9-the-best-way-to-test] (retrieved on 2018-12-30). Visit the URL for the full contents.",entries=>[{module=>"FindBin...

1;
# ABSTRACT: Modules mentioned in Mojolicious Advent Calendar 2017 (day 09)

__END__

=pod

=encoding UTF-8

=head1 NAME

Acme::CPANModules::Import::MojoliciousAdvent::2017_12_09 - Modules mentioned in Mojolicious Advent Calendar 2017 (day 09)

=head1 VERSION

This document describes version 0.001 of Acme::CPANModules::Import::MojoliciousAdvent::2017_12_09 (from Perl distribution Acme-CPANModulesBundle-Import-MojoliciousAdvent-2017), released on 2018-12-30.

=head1 DESCRIPTION

This module is generated by extracting module names mentioned in L<https://mojolicious.io/blog/2017/12/09/day-9-the-best-way-to-test> (retrieved on 2018-12-30). Visit the URL for the full contents.

Modules mentioned in Mojolicious Advent Calendar 2017 (day 09).

This list is generated by extracting module names mentioned in [https://mojolicious.io/blog/2017/12/09/day-9-the-best-way-to-test] (retrieved on 2018-12-30). Visit the URL for the full contents.

=head1 INCLUDED MODULES

=over

=item * L<FindBin>

=item * L<Mojo::Base>

=item * L<Mojo::DOM>



( run in 0.738 second using v1.01-cache-2.11-cpan-4e96b696675 )