想要to_xml和to_json中的行为一致 [英] Want consistent behavior in to_xml and to_json

查看:92
本文介绍了想要to_xml和to_json中的行为一致的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在编写一项服务,该服务对人们之间的联系方式无动于衷.我在http层进行身份验证,因此,如果人们想要xml或json,我会在乎.

我覆盖了to_json和to_xml,但是我对两个都使用了相同的参数. 包括此",不包括"等

其他人如何执行此操作以检查行为是否相同?也许只设置一组参数并在两种方法中都使用它们?或者也许有验证JSON.from_xml(myobject.to_xml)== myobject.to_json的方法?如果我可以帮助的话,我不必重复进行所有测试.

解决方案

所以,我在这里回答我自己的问题.找到了另一个喜欢的人.很好地解释了我在寻找什么.长话短说,我覆盖了serializable_hash

http://api.rubyonrails.org/classes/ActiveRecord/Serialization.html

如何在Rails中覆盖to_json?

PS,我仍然对人们的行为感兴趣.他们是否正在测试JSON和XML格式?您在进行集成测试吗?

So, I'm writing a service that, being in rails, I'm pretty indifferent about how people connect. I do auth at the http layer, so if people want xml or json, I could care less.

I'm overriding to_json and to_xml, but I'm using the same arguments for both. 'include this', 'don't include that', etc.

How are other people doing this to check that the behavior is the same? Maybe just set one set of arguments and use them in both methods? Or maybe have methods that verify the JSON.from_xml(myobject.to_xml) == myobject.to_json? I'd like to not have to do all my tests twice if I can help it.

解决方案

So, I'm answering my own question here. Found another one like it. Has a good explanation of what I was looking for. Long story short, I'm overriding serializable_hash

http://api.rubyonrails.org/classes/ActiveRecord/Serialization.html

How to override to_json in Rails?

PS, I'm still interested in what people are doing. Are they testing JSON and XML formats at all? Are you doing integration tests?

这篇关于想要to_xml和to_json中的行为一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆