簡體   English   中英

無法模擬Rails 4 Griddler gem測試過帳到電子郵件處理器(過帳參數問題)

[英]Cannot simulate Rails 4 Griddler gem test posting to email processor (post params issue)

我們的Griddler模​​型測試工作正常。 例如,我們可以實例化lib / email_processor.rb及其處理。 我們要創建一個控制器測試,以對標准/ email_processor進行端到端發布。

問題是參數沒有通過發布。 我們的基本代碼是:

  @postattr= {to: "hello@hello.com", subject: "a subject", attachments: [
      ActionDispatch::Http::UploadedFile.new({
         filename: 'example_virgin_onetransaction.pdf',
         type: 'application/pdf',
         tempfile: File.new('testfiles/examplefile.pdf")})
  ]}
  post :create, @postattr
  expect(response).to be_success

它在發布到正確的路由並被處理(email.attachments對象為nil除外)時起作用。

我們嘗試過

  • @ postattr.to_json#給出UTF-8中的無效字節序列
  • @ postattr.to_s.to_json#可以,但是沒有參數通過
  • uri編碼json字符串

似乎沒有得到正確處理。 我們錯過了什么?

您的參數似乎僅適用於griddler。 但是在使用griddler-postmark時不正確。 Griddle Postmark適配器接受諸如您的答案之類的參數,然后接受griddler的griddler-postmark預處理參數。 在Rails應用程序中為傳入電子郵件傳遞參數的正確格式如下所示,其中包含griddler-postmark

 attributes = {Subject: "a subject", TextBody: "Hello!",
            ToFull: [{Email: 'to_email@email.com', Name: 'to email'}],
            FromFull: {Email: "from_email@email.com", Name: "from email"},
            Attachments: [{Name: 'filename.pdf',
                           Content: Base64.encode64(fixture_file.read),
                           ContentType: 'application/pdf',
                           ContentLength: fixture_file.size
                          }]}

post :create, attributes

您可能會遇到處理帶有附件的傳入電子郵件的問題。 因此,我添加了以下示例的EmailProcessor類示例

class EmailProcessor

  def initialize(email)
      @email = email
  end

  def process
    if @email.attachments.present?
      attachment = @email.attachments.first
      file = File.new(attachment.original_filename, 'wb')
      file.write attachment.read
      file.flush
      attached_document = AttachedDocument.new(paper: file)
      attached_document.save!
    end
  end
end

希望這對您有幫助:)

是的,似乎電子郵件參數的格式不是很明顯。 地址的往返實際上是列表。

  @post_attr = {Subject: "a subject", TextBody: "Hello!",
                ToFull: [{Email: 'to_email@email.com', Name: 'to email'}],
                FromFull: {Email: "from_email@email.com", Name: "from email"},
                Attachments: [{Name: 'filename.pdf',
                               Content: Base64.encode64(fixture_file.read),
                               ContentType: 'application/pdf',
                               ContentLength: fixture_file.size
                              }]}

希望對別人有幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM