本文介绍了你如何发布到 Capybara 中的 URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

刚从 Cucumber+Webrat 切换到 Cucumber+Capybara,我想知道如何将内容发布到 Capybara 中的 URL.

Just switched from Cucumber+Webrat to Cucumber+Capybara and I am wondering how you can POST content to a URL in Capybara.

在 Cucumber+Webrat 中我能够迈出一步:

In Cucumber+Webrat I was able to have a step:

When /^I send "([^"]*)" to "([^"]*)"$/ do |file, project|
  proj = Project.find(:first, :conditions => "name='#{project}'")
  f = File.new(File.join(::Rails.root.to_s, file))
  visit "project/" + proj.id.to_s + "/upload",
        :post, {:upload_path => File.join(::Rails.root.to_s, file)}
end

但是,Capybara 文档提到:

However, the Capybara documentation mentions:

访问方法只需要一个参数,请求方法为总是 GET.总是 GET.

如何修改我的步骤,以便 Cucumber+Capybara 对 URL 进行 POST?

How do I modify my step so that Cucumber+Capybara does a POST to the URL?

推荐答案

最近我发现 这篇很棒的博文.这对于像 Tony 这样的案例以及您真正想要在您的 cuke 中发布内容的情况非常有用:

More recently I found this great blog post. Which is great for the cases like Tony and where you really want to post something in your cuke:

就我而言,这变成了:

def send_log(file, project)
  proj = Project.find(:first, :conditions => "name='#{project}'")
  f = File.new(File.join(::Rails.root.to_s, file))
  page.driver.post("projects/" + proj.id.to_s + "/log?upload_path=" + f.to_path)
  page.driver.status_code.should eql 200
end

这篇关于你如何发布到 Capybara 中的 URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 15:09