rabl-rails/test/deep_nesting_test.rb

57 lines
1.3 KiB
Ruby
Raw Normal View History

2012-03-02 10:32:11 +00:00
require 'test_helper'
class DeepNestingTest < ActiveSupport::TestCase
class Post
attr_accessor :id, :title
def initialize(id, title)
@id, @title = id, title
end
def comments
[Struct.new(:id, :content).new(1, 'first'), Struct.new(:id, :content).new(2, 'second')]
end
end
setup do
2012-04-20 14:28:34 +00:00
RablRails::Library.reset_instance
2012-03-02 10:32:11 +00:00
@post = Post.new(42, 'I rock !')
@user = User.new(1, 'foobar', 'male')
@user.stub(:posts).and_return([@post])
@context = Context.new
@context.assigns['user'] = @user
@context.virtual_path = 'users/show'
2012-04-15 16:17:49 +00:00
@context.stub(:lookup_context).and_return(mock(:find_template => mock(:source => %{ object :@comment\n attribute :content })))
2012-03-02 10:32:11 +00:00
end
test "compile and render deep nesting template" do
source = %{
object :@user
attributes :id, :name
child :posts do
attribute :title
child :comments do
extends 'comments/show'
end
end
}
2012-07-24 10:13:01 +00:00
assert_equal(MultiJson.encode(:user => {
2012-03-02 10:32:11 +00:00
:id => 1,
:name => 'foobar',
:posts => [{
:title => 'I rock !',
:comments => [
{ :content => 'first' },
{ :content => 'second' }
]
}]
2012-04-20 14:28:34 +00:00
}), RablRails::Library.instance.get_rendered_template(source, @context))
2012-03-02 10:32:11 +00:00
end
end