Skip to content
This repository
Browse code

fix assert_select_email to work on non-multipart emails as well as co…

…nverting the Mail::Body to a string to prevent errors.
  • Loading branch information...
commit 60d358b23348a14447d176fa51624ad5434eb575 1 parent e6fdfd0
Andrew Kaspick authored
4  actionpack/lib/action_dispatch/testing/assertions/selector.rb
@@ -415,9 +415,9 @@ def assert_select_email(&block)
415 415
         assert !deliveries.empty?, "No e-mail in delivery list"
416 416
 
417 417
         for delivery in deliveries
418  
-          for part in delivery.parts
  418
+          for part in (delivery.parts.empty? ? [delivery] : delivery.parts)
419 419
             if part["Content-Type"].to_s =~ /^text\/html\W/
420  
-              root = HTML::Document.new(part.body).root
  420
+              root = HTML::Document.new(part.body.to_s).root
421 421
               assert_select root, ":root", &block
422 422
             end
423 423
           end
19  actionpack/test/controller/assert_select_test.rb
@@ -20,6 +20,15 @@ def test(html)
20 20
     end
21 21
   end
22 22
 
  23
+  class AssertMultipartSelectMailer < ActionMailer::Base
  24
+    def test(options)
  25
+      mail :subject => "Test e-mail", :from => "test@test.host", :to => "test <test@test.host>" do |format|
  26
+        format.text { render :text => options[:text] }
  27
+        format.html { render :text => options[:html] }
  28
+      end
  29
+    end
  30
+  end
  31
+
23 32
   class AssertSelectController < ActionController::Base
24 33
     def response_with=(content)
25 34
       @content = content
@@ -313,6 +322,16 @@ def test_assert_select_email
313 322
     end
314 323
   end
315 324
 
  325
+  def test_assert_select_email_multipart
  326
+    AssertMultipartSelectMailer.test(:html => "<div><p>foo</p><p>bar</p></div>", :text => 'foo bar').deliver
  327
+    assert_select_email do
  328
+      assert_select "div:root" do
  329
+        assert_select "p:first-child", "foo"
  330
+        assert_select "p:last-child", "bar"
  331
+      end
  332
+    end
  333
+  end
  334
+
316 335
   protected
317 336
     def render_html(html)
318 337
       @controller.response_with = html

0 notes on commit 60d358b

Please sign in to comment.
Something went wrong with that request. Please try again.