Class Test::Unit::TestCase
In: lib/feed_validator/assertions.rb
Parent: Object


Public Class methods

Class-level method to quickly create validation tests for a bunch of actions at once in Rails. For example, if you have a FooController with three actions, just add one line to foo_controller_test.rb:

  assert_valid_feed :bar, :baz, :qux


    # File lib/feed_validator/assertions.rb, line 80
80:   def self.assert_valid_feed(*actions)
81:     actions.each do |action|
82:       class_eval "def test_\#{action}_valid_feed\nget :\#{action}\nassert_valid_feed\nend\n"
83:     end
84:   end

Public Instance methods

Assert that feed is valid according the W3C Feed Validation online service. By default, it validates the contents of @response.body, which is set after calling one of the get/post/etc helper methods in rails. You can also pass it a string to be validated. Validation errors, warnings and informations, if any, will be included in the output. The response from the validator service will be cached in the system temp directory to minimize duplicate calls.

For example in Rails, if you have a FooController with an action Bar, put this in foo_controller_test.rb:

  def test_bar_valid_feed
    get :bar


    # File lib/feed_validator/assertions.rb, line 59
59:   def assert_valid_feed(fragment=@response.body)
60:     v =
61:     filename = File.join Dir::tmpdir, 'feed.' + MD5.md5(fragment).to_s
62:     begin
63:       response = filename do |f| Marshal.load(f) end
64:       v.parse(response)
65:         rescue   
66:       unless v.validate_data(fragment)
67:         warn("Sorry! could not validate the feed.")
68:         return assert(true,'')
69:       end
70: filename, 'w+' do |f| Marshal.dump v.response, f end
71:         end
72:     assert(v.valid?, v.valid? ? '' : v.to_s)
73:   end