1

Rails 3 Guides を調べて、Testing Mailers セクションを見ています。

しかし、それらの指示に従うと、それらはバグであり、行われた主張は決して真実ではないのではないかと思います。

これは、ドキュメントの関連セクションです。

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

10.2.2 基本的なテスト ケース

このテストでは、@expected は、テストで使用できる TMail::Mail のインスタンスです。これは ActionMailer::TestCase で定義されています。上記のテストでは、@expected を使用して電子メールを作成し、それをカスタム メーラーによって作成された電子メールでアサートします。招待フィクスチャは電子メールの本文であり、アサートするサンプル コンテンツとして使用されます。ヘルパー read_fixture を使用して、このファイルからコンテンツを読み込みます。

これが、私がそうであると考える理由です:

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">.

違いに気づきましたか?

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

VS

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

それをどのように説明すればよいですか?私の知る限り、それは私のデータの一部ではなく、私の管理下にもありません。

私が使用しているアサーション:

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

これは、ドキュメントで使用されているものと非常によく似ています。

assert_equal @expected.encoded, UserMailer.create_invite('me@example.com', 'friend@example.com', @expected.date).encoded
4

3 に答える 3

1

mochaを使用して「has_message_id?」をスタブ化する 同様の結果があります。私のテストでは、次を追加します。

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

于 2012-09-26T09:58:59.653 に答える
1

きれいではありませんが、私は次のようなことをします:

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

メーラー テスト間で共有したい場合は、次を変更test_helper.rbして追加します。

class ActionMailer::TestCase
    def encode message
        message.encoded.gsub(/Message-ID: <.+>/, '').gsub(/Date: .+/, '')
    end
end
于 2012-09-08T22:40:38.553 に答える
0

私はテストをパスさせます.gsub(/Message-ID: <[\s\S]+>/, '')

したがって、次のように主張します。

assert_equal @expected.encoded.gsub(/Message-ID: <[\s\S]+>/, ''), CardSender.card_update_notification(card, followers, field_updates).encoded.gsub(/Message-ID: <[\s\S]+>/, '')
于 2012-03-13T14:35:53.797 に答える