如何使用 rspec have_selector 方法来验证 XML?
我想做一些基本检查以确保正确生成 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
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.
使用
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.
据我所知,
have_selector
(或have_xpath
)只能来自Webrat或Capybara。您对 John 的评论是正确的,您不需要浏览器模拟器来测试 API。您可能错误地调用了 Webrat 或 Capybara,因为它们使用
visit
而不是get
AFAIK。因此,它们匹配的变量可能从未被页面填充,因此将始终返回 false。如果响应内容类型正确设置为 XML,Rails 应该本地处理 XML 文档。尝试使用
assert_tag
或assert_content
测试助手作为基本检查。我只是使用 Nokogiri 来解析 XHTML 并针对它运行正常的 RSpec 测试:
As far as I can tell the
have_selector
(orhave_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 ofget
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
orassert_content
test helper as a basic check.I would just use Nokogiri to parse the XHTML and run normal RSpec tests against that: