Question

I'm going through the Rails 3 Guides, and looking at Testing Mailers section.

However, following those instructions, I wonder if they are buggy, and the assertion made can never be true.

This is the relevant section in the documentation:

http://guides.rubyonrails.org/testing.html#testing-your-mailers

10.2.2 The Basic Test Case

In this test, @expected is an instance of TMail::Mail that you can use in your tests. It is defined in ActionMailer::TestCase. The test above uses @expected to construct an email, which it then asserts with email created by the custom mailer. The invite fixture is the body of the email and is used as the sample content to assert against. The helper read_fixture is used to read in the content from this file.

This is why I think it is the case:

test_card_update_notification(CardSenderMailerTest) [/Users/victorstan/Sites/ContactMonkey/test/unit/card_sender_mailer_test.rb:21]:

<"Date: Mon, 12 Mar 2012 22:54:38 -0400\r\nFrom: ContactMonkey <support@contactmonkey.com>\r\nTo: test@contactmonkey.com\r\nMessage-ID: <4f5eb6ee903b3_710a3fd4d6034ec8484b1@Victor-Stans-MacBook-Pro.local.mail>\r\nSubject: Bob Smith's ContactMonkey Card Has Been Updated!\r\nMime-Version: 1.0\r\nContent-Type: text/plain;\r\n charset=UTF-8\r\nContent-Transfer-Encoding: 7bit\r\n\r\nHi Bob!\r\nYou asked to be notified when Bob Smith's card had been updated. You can view and download the new card by visiting their profile page:\r\n\r\nhttp://contactmonkey.com/bob\r\n\r\nUpdates at a glance:\r\n\r\n\tCard:\r\n\t\tTitle: Bauws\r\n\t\tOrganization: Fancy Org\r\n\t\tPersonal url: bob\r\n\r\n\tPhone:\r\n\t\tLabel: work\r\n\t\tNumber: 555 555 1234\r\n\r\n\tAddress:\r\n\t\tLabel: work\r\n\t\tStreet: 145 Dovercourt\r\n\t\tStreet2: \r\n\t\tCity: Toronto\r\n\t\tPostalcode: M6J3C5\r\n\t\tRegion: ON\r\n\t\tCountry: Canada\r\n\r\n\r\n\r\nMake it a great day!\r\nContactMonkey\r\n\r\nProblems? Write to us at support@contactmonkey.com, or just reply to this message.\r\nhttp://contactmonkey.com\r\n"> expected but was
<"Date: Mon, 12 Mar 2012 22:54:38 -0400\r\nFrom: ContactMonkey <support@contactmonkey.com>\r\nTo: test@contactmonkey.com\r\nMessage-ID: <4f5eb6eeaeae1_710a3fd4d6034ec848559@Victor-Stans-MacBook-Pro.local.mail>\r\nSubject: Bob Smith's ContactMonkey Card Has Been Updated!\r\nMime-Version: 1.0\r\nContent-Type: text/plain;\r\n charset=UTF-8\r\nContent-Transfer-Encoding: 7bit\r\n\r\nHi Bob!\r\nYou asked to be notified when Bob Smith's card had been updated. You can view and download the new card by visiting their profile page:\r\n\r\nhttp://contactmonkey.com/bob\r\n\r\nUpdates at a glance:\r\n\r\n\tCard:\r\n\t\tTitle: Bauws\r\n\t\tOrganization: Fancy Org\r\n\t\tPersonal url: bob\r\n\r\n\tPhone:\r\n\t\tLabel: work\r\n\t\tNumber: 555 555 1234\r\n\r\n\tAddress:\r\n\t\tLabel: work\r\n\t\tStreet: 145 Dovercourt\r\n\t\tStreet2: \r\n\t\tCity: Toronto\r\n\t\tPostalcode: M6J3C5\r\n\t\tRegion: ON\r\n\t\tCountry: Canada\r\n\r\n\r\n\r\nMake it a great day!\r\nContactMonkey\r\n\r\nProblems? Write to us at support@contactmonkey.com, or just reply to this message.\r\nhttp://contactmonkey.com\r\n">.

Notice the difference?

Message-ID: <4f5eb6ee903b3_710a3fd4d6034ec8484b1@Victor-Stans-MacBook-Pro.local.mail>

VS

Message-ID: <4f5eb6eeaeae1_710a3fd4d6034ec848559@Victor-Stans-MacBook-Pro.local.mail>

How am I supposed to account for that? That's not part of my data, or my control as far as I know.

The assertion I am using:

assert_equal @expected.encoded, CardSenderMailer.card_update_notification(card, followers, field_updates).encoded

Which is very similar to the one used in the documentation:

assert_equal @expected.encoded, UserMailer.create_invite('me@example.com', 'friend@example.com', @expected.date).encoded
Was it helpful?

Solution 3

I make the test pass with .gsub(/Message-ID: <[\s\S]+>/, '')

So that would make the assertion:

assert_equal @expected.encoded.gsub(/Message-ID: <[\s\S]+>/, ''), CardSender.card_update_notification(card, followers, field_updates).encoded.gsub(/Message-ID: <[\s\S]+>/, '')

OTHER TIPS

It is not pretty but I do something like the following:

test "able to mail something" do
    # bunch of @expected statements
    assert_equal encode(@expected), encode(MyMailer.mail(@user))
end  

def encode message
    message.encoded.gsub(/Message-ID: <.+>/, '').gsub(/Date: .+/, '')
end

If you want to share across mailer tests then modify test_helper.rb and add:

class ActionMailer::TestCase
    def encode message
        message.encoded.gsub(/Message-ID: <.+>/, '').gsub(/Date: .+/, '')
    end
end

Using mocha to stub 'has_message_id?' has similar results. In my test, I add:

Mail::Message.any_instance.stubs('has_message_id?').returns(true)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top