我正在阅读Rails 3指南,并查看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
答案 0 :(得分: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
答案 1 :(得分:1)
使用mocha存根'has_message_id?'有类似的结果。在我的测试中,我补充说:
Mail::Message.any_instance.stubs('has_message_id?').returns(true)
答案 2 :(得分: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]+>/, '')