Message ID in mailer is different for same email in unit test #8128

Closed
nathansamson opened this Issue Nov 6, 2012 · 3 comments

Projects

None yet

2 participants

@nathansamson

Following the guide @ http://guides.rubyonrails.org/testing.html#unit-testing
I created a unit test for my mailer.

After some trial and error (my mailers & tests were both wrong, + 1 for unit testing to catch this)
my test almost worked.

The only difference between my test and the actual output was the Message ID.
I suspect that it is based on a hash of the time in milliseconds which obviusly might (WILL) differ in the two cases of the .encoded algorithm.

I didn't find someone else with this problem, so I am thinking of a good solution.

One might be to update the Message-ID generation in test cases to only include the time in seconds or as an hash of the body of the message.

Member

Can you post your test and mailer, please?

Mailer

module MyModule::Core
  class UserMailer < ActionMailer::Base
    default from: "no-reply@dummy.com"

    def changed_email_confirmation(user)
      @user = user

      mail(:to => user.new_email,
           :subject => I18n.t('mailer.user.changed_email_confirmation.subject')) do |format|
        format.html
      end
    end
  end
end

views/mailers/my_module/core/user/changed_email_confirmation.en.html.erb

b

Test

require 'test_helper'

module MyModule::Core
  class UserMailerTest < ActionMailer::TestCase
    test "email changed confirmation" do
      @expected.from = 'no-reply@dummy.com'
      @expected.to = 'new_email@example.org'
      @expected.subject = 'Please activate your email'
      @expected.body = 'b'
      @expected.content_type = 'text/html'

      puts  @expected.inspect
      puts UserMailer.changed_email_confirmation(my_module_core_users(:changed_email)).inspect

      assert_equal @expected.encoded,
                  UserMailer.changed_email_confirmation(my_module_core_users(:changed_email))
                             .encoded
    end
  end
end

Result output

  1) Failure:
test_email_changed_confirmation(MyFriends::Core::UserMailerTest) [/home/nathan/Documenten/NextGenInno/myfriends-collection/myfriends-core/test/functional/my_friends/core/user_mailer_test.rb:15]:
<"Date: Tue, 06 Nov 2012 13:04:07 +0100\r\nFrom: no-reply@dummy.com\r\nTo: new_email@example.org\r\nMessage-ID: <5098fcb7a4de4_7c5febe7e043cc@psi.mail>\r\nSubject: Please activate your email\r\nMime-Version: 1.0\r\nContent-Type: text/html;\r\n charset=UTF-8\r\nContent-Transfer-Encoding: 7bit\r\n\r\nb"> expected but was
<"Date: Tue, 06 Nov 2012 13:04:07 +0100\r\nFrom: no-reply@dummy.com\r\nTo: new_email@example.org\r\nMessage-ID: <5098fcb7a8c06_7c5febe7e04484@psi.mail>\r\nSubject: Please activate your email\r\nMime-Version: 1.0\r\nContent-Type: text/html;\r\n charset=UTF-8\r\nContent-Transfer-Encoding: 7bit\r\n\r\nb">.
Member

Right. This is not a bug in Rails. If you'd like to get help with writing mailer tests, please post to rubyonrails-talk or StackOverflow. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment