[英]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.