2015-05-17 18 views
1

我们的Griddler模​​型测试工作正常。例如我们可以实例化lib/email_processor.rb并处理它。 我们希望创建一个控制器测试,以完成标准/ email_processor的结尾。无法模拟Rails 4 Griddler gem测试发布到邮件处理器(post params issue)

问题是参数不通过帖子。我们的基本代码是:

@postattr= {to: "[email protected]", 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对象是零。

我们试图

  • @ postattr.to_json#给人无效字节顺序UTF-8
  • @ postattr.to_s.to_json#作品,但没有通过PARAMS编码
  • 通过URI json字符串

似乎没有得到正确处理。我们错过了什么?

回答

0

好像它的电子邮件参数的格式是不明显的。往返地址实际上是列表。

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

希望它可以帮助别人

1

你PARAMS似乎是正确使用仅griddler。但使用griddler-postmark时不正确。 Griddle Postmark适配器接受类似于你的答案的参数,然后griddler-postmark为griddler预处理参数。传递params用于在收到的电子邮件中的Rails应用正确的格式是与griddler-邮戳以下

attributes = {Subject: "a subject", TextBody: "Hello!", 
      ToFull: [{Email: '[email protected]', Name: 'to email'}], 
      FromFull: {Email: "[email protected]", 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 

愿望,这将帮助你:)