assert_select_email fix for 3-0-stable #2918

Merged
merged 3 commits into from Sep 7, 2011
View
@@ -1,4 +1,9 @@
-*Rails 3.0.10 (unreleased)*
+*Rails 3.0.11 (unreleased)*
+
+* Fix assert_select_email to work on multipart and non-multipart emails as the method stopped working correctly in Rails 3.x due to changes in the new mail gem.
+
+
+*Rails 3.0.10 (August 31, 2011)*
* Fixes an issue where cache sweepers with only after filters would have no
controller object, it would raise undefined method controller_name for nil [jeroenj]
@@ -569,9 +569,9 @@ def assert_select_email(&block)
assert !deliveries.empty?, "No e-mail in delivery list"
for delivery in deliveries
- for part in delivery.parts
+ for part in (delivery.parts.empty? ? [delivery] : delivery.parts)
@vijaydev

vijaydev Sep 7, 2011

Member

can this be split into two lines?

@akaspick

akaspick Sep 7, 2011

Contributor

It could, but this has already been pulled into master with the current style and I don't see much point in deviating from the other branches if it's not really necessary.

I assume you're looking for something like...
parts = delivery.parts.empty? ? [delivery] : delivery.parts
for part in parts...

If this really needs to be changed, then I'll make another commit.

@jonleighton

jonleighton Sep 7, 2011

Member

I agree it would be better assigned to an ivar on a separate line, but I don't find it so offensive that I think it's worth deviating given I already applied to master.

@jonleighton

jonleighton Sep 7, 2011

Member

But please feel free to refactor on master :)

if part["Content-Type"].to_s =~ /^text\/html\W/
- root = HTML::Document.new(part.body).root
+ root = HTML::Document.new(part.body.to_s).root
assert_select root, ":root", &block
end
end
@@ -22,6 +22,15 @@ def test(html)
end
end
+ class AssertMultipartSelectMailer < ActionMailer::Base
+ def test(options)
+ mail :subject => "Test e-mail", :from => "test@test.host", :to => "test <test@test.host>" do |format|
+ format.text { render :text => options[:text] }
+ format.html { render :text => options[:html] }
+ end
+ end
+ end
+
class AssertSelectController < ActionController::Base
def response_with=(content)
@content = content
@@ -724,6 +733,16 @@ def test_assert_select_email
end
end
+ def test_assert_select_email_multipart
+ AssertMultipartSelectMailer.test(:html => "<div><p>foo</p><p>bar</p></div>", :text => 'foo bar').deliver
+ assert_select_email do
+ assert_select "div:root" do
+ assert_select "p:first-child", "foo"
+ assert_select "p:last-child", "bar"
+ end
+ end
+ end
+
protected
def render_html(html)
@controller.response_with = html