Acme-CPANModulesBundle-Import-MojoliciousAdvent-2017
view release on metacpan or search on metacpan
devdata/https_mojolicious.io_blog_2017_12_20_day-20-practical-testing view on Meta::CPAN
You don't have to worry about overwriting the existing database nor clearing the data at the end of your test.
Further, you can run your tests in <a href="https://metacpan.org/pod/Test::Harness#j<n>">parallel</a> to get a nice speedup in large test suites.</p>
<p>For databases that require a running server you have to be a little more careful, however isolated testing is still very possible.
For example, in Mojo::Pg you can set a <a href="http://mojolicious.org/perldoc/Mojo/Pg#search_path"><code>search_path</code></a> which isolates your test.</p>
<pre><code>my $t = Test::Mojo->new(
'MyApp',
{database => 'postgresql:///test?search_path=test_one'}
);
$pg->db->query('drop schema if exists test_one cascade');
$pg->db->query('create schema test_one');
...
$pg->db->query('drop schema test_one cascade');
</code></pre>
<p>You might have to be careful about when the migration happens too (ie disable <code>auto_migrate</code> and run it manually).
Also this will only isolate the tests per-name, here <code>test_one</code>.
Therefore I recommend you name the path for the name of the test file, this should be both descriptive and unique.
And you have to clean up after yourself otherwise the next time the test is run it will be affected by the remaining data.</p>
<h2>Mocking Helpers</h2>
<p>If you have done any testing you've probably dealt with mocking, but if you haven't, mocking is the act of replacing functionality from essentially unrelated code with test-specific code.
( run in 0.499 second using v1.01-cache-2.11-cpan-49f99fa48dc )