1

Griddler Model テストは問題なく動作しています。たとえば、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 # 動作しますが、パラメータは渡されません
  • json 文字列をエンコードする uri

何も正しく処理されていないようです。私たちは何を逃したのですか?

4

2 に答える 2

1

あなたのパラメータは、グリドラーのみを使用するのに適しているようです。ただし、griddler-postmark を使用すると正しくありません。Griddle Postmark アダプターは、回答のようなパラメーターを受け入れ、次に 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

これがあなたを助けることを願っています:)

于 2015-05-21T06:30:35.497 に答える