aboutsummaryrefslogtreecommitdiffstats
path: root/actionmailer/test/assert_select_email_test.rb
blob: c1a03ac5a3088c50d0075136010d72b1dcc9914f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
require 'abstract_unit'
require 'rails-dom-testing'

class AssertSelectEmailTest < ActionMailer::TestCase
  Assertion = ActiveSupport::TestCase::Assertion

  include Rails::Dom::Testing::Assertions::SelectorAssertions

  class AssertSelectMailer < ActionMailer::Base
    def test(html)
      mail :body => html, :content_type => "text/html",
        :subject => "Test e-mail", :from => "test@test.host", :to => "test <test@test.host>"
    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

  #
  # Test assert_select_email
  #

  def setup
    @response = FakeResponse.new(:html, 'some body text')
  end

  def test_assert_select_email
    assert_raise(Assertion) { assert_select_email {} }
    AssertSelectMailer.test("<div><p>foo</p><p>bar</p></div>").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

  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

    class FakeResponse
      attr_accessor :content_type, :body

      def initialize(content_type, body)
        @content_type, @body = content_type, body
      end
    end
end