[Rt-commit] r17084 - in rt/3.8/trunk: lib/RT/Test

clkao at bestpractical.com clkao at bestpractical.com
Wed Dec 3 06:26:30 EST 2008


Author: clkao
Date: Wed Dec  3 06:26:28 2008
New Revision: 17084

Added:
   rt/3.8/trunk/lib/RT/Test/Email.pm
Modified:
   rt/3.8/trunk/t/approval/basic.t

Log:
* port the email test helpers from jifty.
* make the approval test expect notifications.


Added: rt/3.8/trunk/lib/RT/Test/Email.pm
==============================================================================
--- (empty file)
+++ rt/3.8/trunk/lib/RT/Test/Email.pm	Wed Dec  3 06:26:28 2008
@@ -0,0 +1,77 @@
+use warnings;
+use strict;
+
+package RT::Test::Email;
+use Test::More;
+use Test::Email;
+use Email::Abstract;
+use base 'Exporter';
+our @EXPORT = qw(mail_ok);
+
+RT::Test->set_mail_catcher;
+
+=head1 NAME
+
+RT::Test::Email - 
+
+=head1 SYNOPSIS
+
+  use RT::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 RT, allowing you to expect mail
+notification generated during the block or the test.
+
+=cut
+
+sub mail_ok (&@) {
+    my $code = shift;
+
+    $code->();
+    local $Test::Builder::Level = $Test::Builder::Level + 1;
+    my @msgs = RT::Test->fetch_caught_mails;
+    is(@msgs, @_, "Sent exactly " . @_ . " emails");
+
+    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');
+        diag $te->as_string;
+        bless $te, 'Test::Email';
+        $te->ok($spec, "email matched");
+    }
+    RT::Test->clean_caught_mails;
+}
+
+END {
+    my $Test = Test::More->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 RT::Test->fetch_caught_mails) {
+        diag ((scalar RT::Test->fetch_caught_mails)." uncaught notification email at end of test: ");
+        diag "From: @{[ $_->header('From' ) ]}, Subject: @{[ $_->header('Subject') ]}"
+            for RT::Test->fetch_caught_mails;
+        die;
+    }
+}
+
+1;
+

Modified: rt/3.8/trunk/t/approval/basic.t
==============================================================================
--- rt/3.8/trunk/t/approval/basic.t	(original)
+++ rt/3.8/trunk/t/approval/basic.t	Wed Dec  3 06:26:28 2008
@@ -1,11 +1,20 @@
 
 use strict;
 use warnings;
-use Test::More; 
-plan tests => 20;
+use Test::More;
+
+BEGIN {
+    eval { require Email::Abstract; require Test::Email; 1 }
+        or plan skip_all => 'require Email::Abstract and Test::Email';
+}
+plan tests => 22;
+
 use RT;
 use RT::Test;
+use RT::Test::Email;
+
 RT->Config->Set( LogToScreen => 'debug' );
+
 my ($baseurl, $m) = RT::Test->started_ok;
 
 my ($user_a, $user_b) = (RT::User->new($RT::SystemUser), RT::User->new($RT::SystemUser));
@@ -73,9 +82,18 @@
 ok ($scrip->ActionObj->Id, "Created the scrip action");
 
 my $t = RT::Ticket->new($RT::SystemUser);
-my($tid, $ttrans, $tmsg) = $t->Create(Subject => "PO for stationary",
-           Owner => "root", Requestor => 'minion',
-           Queue => $q->Id);
+my ($tid, $ttrans, $tmsg);
+
+mail_ok {
+    ($tid, $ttrans, $tmsg) =
+        $t->Create(Subject => "PO for stationary",
+                   Owner => "root", Requestor => 'minion',
+                   Queue => $q->Id);
+} { from => qr/PO via RT/,
+    to => 'minion at company.com',
+    subject => qr/PO for stationary/,
+    body => qr/automatically generated in response/
+};
 
 ok ($tid,$tmsg);
 
@@ -97,7 +115,7 @@
 is_deeply([ $t->Status, $dependson_cfo->Status, $dependson_ceo->Status ],
           [ 'new', 'new', 'new']);
 
-$dependson_cfo->SetStatus( Status => 'resolved' );#, Force => 1);
+$dependson_cfo->SetStatus( Status => 'resolved' );
 
 is ($t->DependsOn->Count, 1, "still depends only on the CEO approval");
 is ($t->ReferredToBy->Count,2, "referred to by the two tickets");


More information about the Rt-commit mailing list