18

email-spec gem で rspec を使用しています。私はやろうとしています:

last_delivery = ActionMailer::Base.deliveries.last
last_delivery.body.should include "This is the text of the email"

しかし、それは機能しません。本文、テキスト バージョンと言う方法はありますか? コンテンツ タイプ: テキスト/テキスト?

ありがとう

4

8 に答える 8

35

Body は実際にはMail::Bodyインスタンスです。その上で raw_source を呼び出すと、うまくいくはずです。

last_delivery = ActionMailer::Base.deliveries.last
last_delivery.body.raw_source.should include "This is the text of the email"
于 2011-03-22T11:31:20.590 に答える
21

上記のすべてのオプションを試してうまくいかなかった後 (Rails 3.2.13)、Ruby ガイド(セクション 6 は TestUnit でのテストに関するもの) でいくつかの情報を見つけ、必要に応じて正確に動作するようにしました。

last_delivery = ActionMailer::Base.deliveries.last
last_delivery.encoded.should include("This is the text of the email")

それが役立つことを願っています!

于 2013-06-11T23:38:40.333 に答える
5

あなたはそれを呼び出すことができます#to_s

last_delivery.to_s.should include "This is the text of the email"

マルチパートメールの場合:

last_delivery.first.to_s.should include "This is the text of the email"

テスト済みで、Rails 4 で動作していることがわかった

于 2013-10-31T10:55:47.013 に答える
0

Rails 4 では、デフォルトでlast_delivery.body.should include "This is the text of the email"問題なく動作します。

于 2014-08-27T00:14:45.620 に答える
-2
expect(last_delivery).to have_body_text(/This is the text of the email/)

ソース

于 2014-04-02T09:43:36.667 に答える