如何使用 rspec have_selector 方法来验证 XML?

发布于 2024-12-18 10:49:19 字数 979 浏览 0 评论 0原文

我想做一些基本检查以确保正确生成 XML 站点地图,但 have_selector 似乎无法检测标签:

require 'spec_helper'

describe SitemapController do

  render_views

  before(:all) do
    # code to generate factory data
    # ...
  end

  # illustrating the problem
  it "should be able detect nodes that are definitely present" do
    get :index
    response.should have_selector('url')
  end
end

每次运行测试时都会出现以下错误

RSpec::Expectations::ExpectationNotMetError: expected css "url" to return something

:正在生成站点地图,当我调试 RSpec 测试并查看 response 对象时,我可以看到 xml 内容:

ruby-1.9.2-p180 :001 > response.body
 => "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\n  <url>\n    <loc>http://test.host/panel ... 

我的站点地图由 SitemapController 生成,视图位于views/sitemap/index.builder.xml

为什么 have_selector 没有启动?

I want to do some basic checking to ensure that an XML sitemap is being produced correctly but have_selector doesn't seem to be able to detect tags:

require 'spec_helper'

describe SitemapController do

  render_views

  before(:all) do
    # code to generate factory data
    # ...
  end

  # illustrating the problem
  it "should be able detect nodes that are definitely present" do
    get :index
    response.should have_selector('url')
  end
end

Every time I run the test I get the following error:

RSpec::Expectations::ExpectationNotMetError: expected css "url" to return something

The sitemap is being produced and when I debug the RSpec test and look at the response object I can see xml content:

ruby-1.9.2-p180 :001 > response.body
 => "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\n  <url>\n    <loc>http://test.host/panel ... 

My sitemap is produced by the SitemapController and the view is located in views/sitemap/index.builder.xml.

Why isn't have_selector kicking in?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

疑心病 2024-12-25 10:49:19

Capybara 不支持 XML 响应。它始终使用 Nokogiri::HTML 来解析内容,这在给定 XML 时会产生意外的结果。

已请求添加 XML 支持,但被 Capybara 的维护者拒绝。

Capybara doesn't support XML responses. It always uses Nokogiri::HTML to parse the content, which produces unexpected results when given XML.

Adding XML support has been requested but was rejected by Capybara's maintainer.

飞烟轻若梦 2024-12-25 10:49:19

使用 should have_xpath('//url') 代替。 have_selector 用于 CSS。

有关更多信息,请参阅水豚自述文件

Use should have_xpath('//url') instead. have_selector is for CSS.

See the Capybara README for more info.

青春如此纠结 2024-12-25 10:49:19

据我所知,have_selector(或have_xpath)只能来自Webrat或Capybara。您对 John 的评论是正确的,您不需要浏览器模拟器来测试 API。

您可能错误地调用了 Webrat 或 Capybara,因为它们使用 visit 而不是 get AFAIK。因此,它们匹配的变量可能从未被页面填充,因此将始终返回 false。

如果响应内容类型正确设置为 XML,Rails 应该本地处理 XML 文档。尝试使用 assert_tagassert_content 测试助手作为基本检查。

我只是使用 Nokogiri 来解析 XHTML 并针对它运行正常的 RSpec 测试:

xml = Nokogiri::XML(response.body)
xml.css("expression").size.should == 1

As far as I can tell the have_selector (or have_xpath) can only come from Webrat or Capybara. You're right in your comment to John that you don't need a browser simulator to test the API.

You may be invoking Webrat or Capybara by mistake as they use visit instead of get AFAIK. So the variables they match against may never have been populated by the page and so will always return false.

Rails should handle the XML document natively if the response content-type is correctly set to XML. Try using the assert_tag or assert_content test helper as a basic check.

I would just use Nokogiri to parse the XHTML and run normal RSpec tests against that:

xml = Nokogiri::XML(response.body)
xml.css("expression").size.should == 1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文