Archive for the ‘xml’ Category
Rails 2.x automatically dasherizes ActiveRecord inheritors when to_xml is called, but if you want to deal directly with Builder::XmlMarkup you have to go through some hoops to dasherize your element names.
I solved this problem today with a simple subclass. Here's the code:
[sourcecode language='ruby']
# Creates a Builder::Markup implementation that dasherizes all element and attribute names
# Use this just like you would Builder::XmlMarkup
class DasherizingBuilder < Builder::XmlMarkup
def _start_tag(sym, attrs, end_too=false)
super(sym.to_s.dasherize, attrs, end_too)
end
def _end_tag(sym)
super(sym.to_s.dasherize)
end
def _insert_attributes(attrs, order=[])
return if attrs.nil?
new_order= []
order.each {|item| new_order
1 Comment »
Recently I was writing custom Xml exporting for some of our Rails models. I favor TDD over just shoot-from-the-hip development, so I had to write some tests. For testing the validity of the XML, I chose XPath for its simplicity.
As I added functionality (and thereby added tests) I noticed that my tests were becoming increasingly redundant, and that Test::Unit's syntax was making my test files ugly and more importantly hard to read. As I started DRY-ing up the individual tests and trying to find better ways to share code and make the tests more succinct, I noticed that I was driving very close to creating my own mini-DSL for testing Xml with Xpath.
My Requirements for Xml Exporting
A required element must always exist in the resulting Xml regardless of the model's state
An optional element:
Must exist when the model meets a certain state condition (i.e. the member is not null)
Must not exist when that condition is not met
A DSL That Meets My Testing Needs
As mentioned earlier, this DSL evolved from repeated refactoring my tests in order to make them more clear. Here is an example of an XPath-based Xml test:
[sourcecode lang='ruby']
xpath_tests_for @video_library do
# ensure that the Xml always contains:
#
No Comments »
Inspired by an article on adding XPath matching to RSpec. I'm not using RSpec currently, and I also have a need for this functionality in my tests, so I decided to a helper for use in Test::Unit to achieve XPath matching in my tests.
Add a new helper method:
Add the following code to your test/test_helper.rb in order to create the assert_has_xpath method.
[sourcecode lang='ruby']
#test/test_helper.rb
require 'rexml/document'
# Asserts that the specified xpath matches
# at least once in the given document
def assert_has_xpath (xpath, doc)
doc = doc.is_a?(REXML::Document) ? doc : REXML::Document.new(doc)
match = REXML::XPath.match doc, xpath
assert !match.empty?, "Missing xpath '#{xpath}' in document: #{doc}"
end
[/sourcecode]
Optionally, you can add the above to a module and include it in your test_helper, or in your individual tests.
Use the helper in your tests:
Now, in your tests, you can do something like the following
[sourcecode lang='ruby']
#model.rb
def to_xml options = {}
xml = options[:builder] ||= Builder::XmlMarkup.new(options)
xml.toys{
xml.toy "ball"
xml.toy "iPhone"
}
end
#model_test.rb
def test_create_xml_creates_toys
model = Model.new
xml = model.to_xml
assert_has_xpath "/toys[toy='iPhone']", xml
end
[/sourcecode]
And voila! You now have XPath testing in your Test::Unit tests.
No Comments »