简体   繁体   English

为返回XML的控制器动作编写规范— Ruby on Rails

[英]Writing spec for controller actions that return XML — Ruby on Rails

In my controller I have some methods which return json as well as xml data. 在我的控制器中,我有一些返回json以及xml数据的方法。 I need to test those actions. 我需要测试这些动作。 For testing the json part I found this reference and it is working fine. 为了测试json部分,我找到了这个参考 ,它工作正常。 Now I am need of testing the actions for the XML part. 现在,我需要测试XML部分的操作。 Anyone who can help or suggest something are most welcome I've been scratching my head for an hour now but to no avail. 任何能提供帮助或建议的人都非常欢迎,我已经挠头一个小时了,但无济于事。 Please help. 请帮忙。 Thanks in advance. 提前致谢。

when you request your controller you specify the format you want. 当您请求控制器时,您可以指定所需的格式。 After parse the result with Nokogiri and see if your XML is valid or not 用Nokogiri解析结果后,查看您的XML是否有效

it 'should has tag hello' do
  get :index :format => :xml
  Nokogiri::XML(response.body).should has_tag('hello')
ebd

@shingara thanks for the answer. @shingara感谢您的回答。 But I wanted to do it without using any extra tools and just rspec. 但我想做到这一点,而无需使用任何额外的工具,而只是使用rspec。 I found the solution here . 我在这里找到了解决方案。 Read the complete post. 阅读完整的帖子。 It has what I want and also a bit demo of Hpricot. 它具有我想要的功能,还包含Hpricot的演示。 Well anyways thanks for answering. 好吧,谢谢您的回答。

it 'should has tag hello' do
  get :index :format => :xml
  response.should have_tag("hello",'value inside hello tag')
end

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Ruby on Rails和RSpec编写助手规范 - Writing a spec for helper with Ruby on Rails and RSpec 在功能规范中,将用户设置为 Warden 以在 Ruby on Rails 应用程序中的操作中进行重定向 - In a Feature spec, set the user with Warden for redirects in actions in a Ruby on Rails application 如何在Ruby on Rails中生成控制器动作数组? - How to generate an array of controller actions in Ruby on Rails? Ruby on Rails:一个控制器中动作的默认模板 - Ruby on Rails: Default template for actions in one controller Ruby on rails - 如何使控制器与ajax同时执行2个动作? - Ruby on rails - How to make controller execute 2 actions simultaneously with ajax? Ruby on Rails:将不同控制器的动作包含到另一个控制器的索引视图中 - Ruby on Rails: Including actions of different controllers into index view of another controller 在Ruby on Rails中,如何将控制器中的所有动作映射到root? - In Ruby on Rails, how can I map all actions in a controller to root? 如何在Rails的ruby中的单个页面选项卡中调用多个控制器动作 - How to call multiple controller actions into the single page tabs in ruby on rails CSS和JS无法在Rails的一个控制器ruby中以两个动作加载 - CSS and JS not loading with two actions in one controller ruby on rails 向控制器Ruby on Rails中的所有相关动作添加活动类 - Add active class to all relative actions inside controller Ruby on Rails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM