Class: Test::Unit::TestCase
- Inherits:
-
Object
- Object
- Test::Unit::TestCase
- Defined in:
- lib/feed_validator/assertions.rb
Class Method Summary collapse
-
.assert_valid_feed(*actions) ⇒ Object
Class-level method to quickly create validation tests for a bunch of actions at once in Rails.
Instance Method Summary collapse
-
#assert_valid_feed(fragment = @response.body) ⇒ Object
Assert that feed is valid according the W3C Feed Validation online service.
Class Method Details
.assert_valid_feed(*actions) ⇒ Object
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
80 81 82 83 84 85 86 87 88 89 |
# File 'lib/feed_validator/assertions.rb', line 80 def self.assert_valid_feed(*actions) actions.each do |action| class_eval <<-EOF def test_#{action}_valid_feed get :#{action} assert_valid_feed end EOF end end |
Instance Method Details
#assert_valid_feed(fragment = @response.body) ⇒ Object
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
get :bar
assert_valid_feed
end
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/feed_validator/assertions.rb', line 59 def assert_valid_feed(fragment=@response.body) v = W3C::FeedValidator.new() filename = File.join Dir::tmpdir, 'feed.' + Digest::MD5.hexdigest(fragment).to_s begin response = File.open filename do |f| Marshal.load(f) end v.parse(response) rescue unless v.validate_data(fragment) warn("Sorry! could not validate the feed.") return assert(true,'') end File.open filename, 'w+' do |f| Marshal.dump v.response, f end end assert(v.valid?, v.valid? ? '' : v.to_s) end |