From dc66b43638107b586b9dfc694bc094ebece051d0 Mon Sep 17 00:00:00 2001 From: ccocchi Date: Wed, 12 Sep 2012 01:33:04 +0200 Subject: [PATCH] Replace Struct by Class to avoid stubbing respond_to? Add some tests with local methods --- test/deep_nesting_test.rb | 1 - test/renderers/json_renderer_test.rb | 25 ++++++++++++++++++------- test/test_helper.rb | 13 ++++++++++--- 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/test/deep_nesting_test.rb b/test/deep_nesting_test.rb index e689233..4292877 100644 --- a/test/deep_nesting_test.rb +++ b/test/deep_nesting_test.rb @@ -19,7 +19,6 @@ class DeepNestingTest < ActiveSupport::TestCase @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.assigns['user'] = @user diff --git a/test/renderers/json_renderer_test.rb b/test/renderers/json_renderer_test.rb index c04052d..b2ab458 100644 --- a/test/renderers/json_renderer_test.rb +++ b/test/renderers/json_renderer_test.rb @@ -4,7 +4,6 @@ class TestJsonRenderer < ActiveSupport::TestCase setup do @data = User.new(1, 'foobar', 'male') - @data.stub(:respond_to?).with(:each).and_return(false) @context = Context.new @context.assigns['data'] = @data @@ -28,6 +27,13 @@ class TestJsonRenderer < ActiveSupport::TestCase assert_equal %q([{}]), render_json_output end + test "render object with local methods (used by decent_exposure)" do + @context.stub(:user).and_return(@data) + @template.data = :user + @template.source = { :id => :id } + assert_equal %q({"id":1}), render_json_output + end + test "render single object attributes" do @template.source = { :id => :id, :name => :name } assert_equal %q({"id":1,"name":"foobar"}), render_json_output @@ -44,6 +50,12 @@ class TestJsonRenderer < ActiveSupport::TestCase assert_equal %q({"author":{"name":"foobar"}}), render_json_output end + test "render child with local methods (used by decent_exposure)" do + @context.stub(:user).and_return(@data) + @template.source = { :author => { :_data => :user, :name => :name } } + assert_equal %q({"author":{"name":"foobar"}}), render_json_output + end + test "render glued attributes from single object" do @template.source = { :_glue0 => { :_data => :@data, :name => :name } } assert_equal %q({"name":"foobar"}), render_json_output @@ -75,7 +87,7 @@ class TestJsonRenderer < ActiveSupport::TestCase @template.source = { :name => [condition, proc] } assert_equal %q({}), render_json_output end - + test "node with context method call" do @context.stub(:respond_to?).with(:context_method).and_return(true) @context.stub(:context_method).and_return('marty') @@ -86,7 +98,6 @@ class TestJsonRenderer < ActiveSupport::TestCase test "partial should be evaluated at rendering time" do # Set assigns - @data.stub(:respond_to?).with(:empty?).and_return(false) @context.assigns['user'] = @data # Stub Library#get @@ -114,17 +125,17 @@ class TestJsonRenderer < ActiveSupport::TestCase assert_equal %q({"users":[]}), render_json_output end - + test "render object with root node" do @template.root_name = :author @template.source = { :id => :id, :name => :name } - assert_equal %q({"author":{"id":1,"name":"foobar"}}), render_json_output + assert_equal %q({"author":{"id":1,"name":"foobar"}}), render_json_output end - + test "render object with root options set to false" do RablRails.include_json_root = false @template.root_name = :author @template.source = { :id => :id, :name => :name } - assert_equal %q({"id":1,"name":"foobar"}), render_json_output + assert_equal %q({"id":1,"name":"foobar"}), render_json_output end end \ No newline at end of file diff --git a/test/test_helper.rb b/test/test_helper.rb index 26b144e..7468f73 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -32,12 +32,12 @@ end class Context attr_writer :virtual_path - + def initialize @_assigns = {} @virtual_path = nil end - + def assigns @_assigns end @@ -47,4 +47,11 @@ class Context end end -User = Struct.new(:id, :name, :sex) \ No newline at end of file +class User + attr_accessor :id, :name, :sex + def initialize(id=nil, name=nil, sex=nil) + @id = id + @name = name + @sex = sex + end +end \ No newline at end of file