Ruby on rails rspec/capybara:如何模拟传入的POST请求?(机架测试无效)

Ruby on rails rspec/capybara:如何模拟传入的POST请求?(机架测试无效),ruby-on-rails,ruby-on-rails-3,rspec2,capybara,cloudmailin,Ruby On Rails,Ruby On Rails 3,Rspec2,Capybara,Cloudmailin,我需要通过Cloudmailin的POST请求以多部分formdata的形式接收传入的电子邮件。该职位类似如下: Parameters: {"to"=>"<email@exmaple.comt>", "from"=>"whomever@example", "subject"=>"my awesome subject line.... 参数:{“到”=>,“从”=>“whomever@example“,”主题“=>”我最棒的主题行。。。。 实际上,接收和解析电子邮

我需要通过Cloudmailin的POST请求以多部分formdata的形式接收传入的电子邮件。该职位类似如下:

Parameters: {"to"=>"<email@exmaple.comt>", "from"=>"whomever@example", "subject"=>"my awesome subject line....
参数:{“到”=>,“从”=>“whomever@example“,”主题“=>”我最棒的主题行。。。。
实际上,接收和解析电子邮件非常简单,因为电子邮件只是以params:params[:to]、params[:from]等形式发布。但是,如何在rails中模拟此发布请求


我构建了一个虚拟rails应用程序来测试Cloudmailin,所以我有一个实际的请求。但是,它是一个6k字符的文件,所以我想加载这个文件作为POST请求的参数。我尝试使用构建的rails POST和POST\u通过\u重定向方法加载一个文件,但它会转义所有参数(\“to\”),这是不好的。有什么想法吗?

一个简单的方法可能是在capybara中执行脚本。只需确保使用
@javascript
标记,然后在应用程序中加载安装了jQuery的任何页面(从技术上讲,您不需要这个,但这要简单得多。然后:

When /^I get a post request from Cloudmailin$/ do
  visit '/some/page/with/jquery'
  page.execute_script(%{$.post("/some/path?to=some_email&etc=etc");})
end
还有一个简单的
post
capybara方法,但我不太确定它是如何工作的。可能值得研究一下。

因此,我最终做了:

@parameters = { "x_to_header"=>"<#{ @detail.info }>",
                "to"=>"<#{ @account.slug }@cloudmailin.net>",
                "from"=>"#{ @member.email }",
                "subject"=>"meeting on Monday",
                "plain"=>"here is my message\nand this is a new line\n\n\nand two new lines\n\n\n\nand a third new line"
              }

似乎现在已经完成了这项工作

我昨晚在更新我自己的Rails 3.2.8测试代码时看到了这个答案,它使用了Mail gem,我想我会分享我的发现。该测试代码用于一个应用程序,它需要从Cloudmailin获取帖子,然后使用Desive处理它来创建一个新用户,然后发送邮件向该用户确认,然后用户可以按照该确认选择密码。以下是我的控制器规范:

require 'spec_helper'

describe ThankyouByEmailController do

  message1 = Mail.new do 

    from "Frommy McFromerton <frommy.mcfrommerton@gmail.com>"
    to "toey.receivesalot@gmail.com"
    subject "cloudmailin test"
    body 'something'

    text_part do
      body 'Here is the attachment you wanted'
    end

    html_part do
      content_type 'text/html; charset=UTF-8'
      body '<h1>Funky Title</h1><p>Here is the attachment you wanted</p>'
    end
  end

  describe "creating new users" do

    describe "unregistered FROM sender and Unregistered TO receiver" do

      it "should create 2 new users" do
        lambda do
          post :create, :message => "#{@message1}"
        end.should change(User, :count).by(2)
      end
    end
  end
end
require'spec\u helper'
请用EmailController描述谢谢
message1=Mail.new do
从“Fromy McFromerton”
“托伊”。receivesalot@gmail.com"
主题“cloudmailin测试”
身体“某物”
文字部分做什么
正文“这是您想要的附件”
结束
你是做什么的
内容类型“text/html;charset=UTF-8”
body'时髦的标题这是您想要的附件

' 结束 结束 描述“创建新用户”是什么 描述“从发送者处注销,从接收者处注销”吗 它“应该创建2个新用户”吗 lambda do post:create,:message=>“#{@message1}” end.should更改(用户:count).by(2) 结束 结束 结束 结束
希望这能清理您自己的测试。对于其他对测试mail gem感兴趣的人来说,mikel的文档在这方面也取得了长足的进步:


特别感谢您的评论。我相信fakeweb是为了测试从外部API获取的。我需要测试传入的请求。事实上,这不是真的。我只需要将一些参数发布到控制器,其余的测试将确保创建了正确的邮件。抱歉,我可能应该睡觉:)碰巧把这个问题看作是一个“相关问题”,所以把它联系起来——很适合这里:谢谢Ramon的建议。不过,我只是手动建立一个参数散列,然后发布API端点:post'/incoming',@parameters(我想这是目前最简单的解决方案)所以您使用的是capybara的post方法?我认为它只是一个rails瘦的方法?对。该
post
方法不是capybara的一部分。请参阅听起来也是一个简洁而简单的想法这会与其他函数一起工作吗(“get”例如)?
require 'spec_helper'

describe ThankyouByEmailController do

  message1 = Mail.new do 

    from "Frommy McFromerton <frommy.mcfrommerton@gmail.com>"
    to "toey.receivesalot@gmail.com"
    subject "cloudmailin test"
    body 'something'

    text_part do
      body 'Here is the attachment you wanted'
    end

    html_part do
      content_type 'text/html; charset=UTF-8'
      body '<h1>Funky Title</h1><p>Here is the attachment you wanted</p>'
    end
  end

  describe "creating new users" do

    describe "unregistered FROM sender and Unregistered TO receiver" do

      it "should create 2 new users" do
        lambda do
          post :create, :message => "#{@message1}"
        end.should change(User, :count).by(2)
      end
    end
  end
end