[Jifty-commit] r5963 - in jifty/trunk: lib/Jifty/Test

Jifty commits jifty-commit at lists.jifty.org
Thu Oct 23 08:24:22 EDT 2008


Author: clkao
Date: Thu Oct 23 08:24:19 2008
New Revision: 5963

Added:
   jifty/trunk/lib/Jifty/Test/Email.pm
Modified:
   jifty/trunk/Makefile.PL

Log:
test helper module for matching notification email sent during test.


Modified: jifty/trunk/Makefile.PL
==============================================================================
--- jifty/trunk/Makefile.PL	(original)
+++ jifty/trunk/Makefile.PL	Thu Oct 23 08:24:19 2008
@@ -116,6 +116,8 @@
         recommends('Test::MockObject' => '1.07'),
         recommends('Module::Refresh' => '0.09'),
         recommends('Test::WWW::Declare' => '0.01'),
+        recommends('Test::Email'),
+        recommends('Email::Abstract'),
     ],
     'Development of the jifty framework' => [
         -default => 0,

Added: jifty/trunk/lib/Jifty/Test/Email.pm
==============================================================================
--- (empty file)
+++ jifty/trunk/lib/Jifty/Test/Email.pm	Thu Oct 23 08:24:19 2008
@@ -0,0 +1,73 @@
+use warnings;
+use strict;
+
+package Jifty::Test::Email;
+use Test::More;
+use Test::Email;
+use Email::Abstract;
+
+use base 'Exporter';
+our @EXPORT = qw(mail_ok);
+
+=head1 NAME
+
+Jifty::Test::Email - 
+
+=head1 SYNOPSIS
+
+  use Jifty::Test::Email;
+
+  mail_ok {
+    # ... code
+
+  }, { from => 'admin at localhost', body => qr('hello') },
+     { from => 'admin at localhost', body => qr('hello again') };
+
+  # ... more code
+
+  # XXX: not yet
+  mail_sent_ok { from => 'admin at localhost', body => qr('hello') };
+
+  # you should expect all mails by the end of the test
+
+
+=head1 DESCRIPTION
+
+This is a test helper module for jifty, allowing you to expect mail
+notification generated during the block or the test.
+
+=cut
+
+sub mail_ok (&@) {
+    my $code = shift;
+    # XXX. ensure mailbox is empty; but make sure the test count is correct
+    $code->();
+    local $Test::Builder::Level = $Test::Builder::Level + 1;
+    my @msgs = Jifty::Test->messages;
+    for my $spec (@_) {
+        my $msg = shift @msgs
+            or ok(0, 'Expecting message but none found.'), next;
+
+        my $te = Email::Abstract->new($msg)->cast('MIME::Entity');
+        bless $te, 'Test::Email';
+        $te->ok($spec, "email matched");
+    }
+    Jifty::Test->setup_mailbox;
+}
+
+END {
+    my $Test = Jifty::Test->builder;
+    # Such a hack -- try to detect if this is a forked copy and don't
+    # do cleanup in that case.
+    return if $Test->{Original_Pid} != $$;
+
+    if (scalar Jifty::Test->messages) {
+        diag ((scalar Jifty::Test->messages)." uncaught notification email at end of test: ");
+        diag "From: @{[ $_->header('From' ) ]}, Subject: @{[ $_->header('Subject') ]}"
+            for Jifty::Test->messages;
+        die;
+    }
+}
+
+1;
+


More information about the Jifty-commit mailing list