rabl-rails/test/deep_nesting_test.rb

59 lines
1.5 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])
@user.stub(:respond_to?).with(:each).and_return(false)
@context = Context.new
@context.stub(:instance_variable_get).with(:@user).and_return(@user)
@context.stub(:instance_variable_get).with(:@virtual_path).and_return('users/show')
2012-03-15 14:12:52 +00:00
@context.stub(:instance_variable_get).with(:@_assigns).and_return({})
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
}
assert_equal(ActiveSupport::JSON.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