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_hashhttp://api.rubyonrails.org/classes/ActiveRecord/Serialization.htmlHow 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中的行为一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-31 07:05