내 Rails 응용 프로그램에서 SendGrid의 SMTP API을 사용하여 전자 메일을 보냅니다. 그러나 RSpec을 사용하여 전자 메일 헤더 ("X-SMTPAPI")를 테스트하는 데 문제가 있습니다. 여기 RSpec을 사용하여 전자 메일 헤더를 테스트하는 방법
은 이메일 (ActionMailer :: Base.deliveries에서 검색)의 모습입니다 :ActionMailer::Base.deliveries.last.to.should include("[email protected]")
나는 또한 다양한 시도했다 :
#<Mail::Message:2189335760, Multipart: false, Headers:
<Date: Tue, 20 Dec 2011 16:14:25 +0800>,
<From: "Acme Inc" <[email protected]>>,
<To: [email protected]>,
<Message-ID: <[email protected]>>,
<Subject: Your Acme order>, <Mime-Version: 1.0>,
<Content-Type: text/plain>, <Content-Transfer-Encoding: 7bit>,
<X-SMTPAPI: {"sub":{"|last_name|":[Foo],"|first_name|":[Bar]},"to":["[email protected]"]}>>
여기 (실패) 내 사양 코드입니다 메서드를 사용하여 머리글 ("X-SMTPAPI")을 검색했지만 작동하지 않았습니다.
mail = ActionMailer::Base.deliveries.last
mail.headers("X-SMTPAPI") #NoMethodError: undefined method `each_pair' for "X-SMTPAPI":String
Help?
이 밝혀
업데이트 (대답), I는 이메일 헤더의 값을 검색하기 위해이 작업을 수행 할 수 있습니다
mail.header['X-SMTPAPI'].value
그러나, 반환 값은 JSON 형식입니다. 그럼,이 할 필요는 그것을 해독하는 것입니다
sendgrid_header["to"]
이메일 주소의 배열을 검색하기 :
sendgrid_header = ActiveSupport::JSON.decode(mail.header['X-SMTPAPI'].value)
나는이 작업을 수행 할 수있는 해시를 반환합니다.
감사합니다. email_spec 보석을 확인했습니다. "deliver_to"매처는 "to"매처 ("[email protected]"대신 "[email protected]"을 반환)와 비슷하며 "have_header"매처는 단순히 전체 헤더를 반환합니다. 어쨌든, 나는 이것에 대한 해결책을 발견하고 그것을 올릴 것입니다. 감사! –