Skip to content

Instantly share code, notes, and snippets.

@nesquena
Created November 27, 2012 23:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nesquena/4158034 to your computer and use it in GitHub Desktop.
Save nesquena/4158034 to your computer and use it in GitHub Desktop.
RABL Testing Reference

Common Misunderstandings

Serialization should be testable

Agreed and with RABL serialization is testable both in and outside of a web request context. Check out the other gists in this file for examples using Rabl.render to do standalone unit testing.

RABL can't be used for generating push notifications or in background jobs

Not at all true, I use RABL for these purposes in all of my applications. Simply use Rabl.render to render templates in any context.

RABL is linked inextricably to or requires ActionView, Rails, or a controller

See above. Simply not at all true. Simply use Rabl.render to render templates in any context.

Object serialization doesn't belong in templates

JSON and XML are data exchange formats and they do fundamentally represent a view of your data not unlike an HTML view or any other view format. JSON rendering in an API is a view of your data, there's no way around that. Moreso, even in other contexts outside of a controller the template is rendering an object into a representation. A ruby-based DSL such as RABL is a great way to create these representations. In addition, with RABL you can render into JSON, XML, Plist, MsgPack, et al using the same consistent templating language.

RABL templates can become complicated

As with all views, templates should not contain intricate object and model logic or complex conditionals. Views are intended to be simple representations of your objects and this is where RABL fits well into the picture. If you are worried about RABL views getting complex, you should apply best practices and extract RABL partials to reduce code duplication as with any view and use presenters to organize the code correctly.

# Test the API response (if template is used an API generation)
require File.expand_path(File.dirname(__FILE__) + '/../test_config.rb')
describe "Post Controller" do
setup do
@post = Factory.generate(:body => "foo")
get :show, :id => @post.id
@result = JSON.parse(last_response.body)
end
it "should render post body" do
assert_equal "foo", @result['post']['body']
end
it "should be have other keys" do
assert_same_elements ['body', 'user_id', 'title'], @result['post'].keys
end
end
# Test the RABL object serialization (if template is used for serializing outside API generation)
require File.expand_path(File.dirname(__FILE__) + '/../test_config.rb')
describe "Post Serialization" do
setup do
@post = Factory.generate(:body => "foo")
@result = Rabl::Renderer.render(@post, 'posts/show', :format => :hash)
end
it "should render post body" do
assert_equal "foo", @result['post']['body']
end
it "should be have other keys" do
assert_same_elements ['body', 'user_id', 'title'], @result['post'].keys
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment