Replace Struct by Class to avoid stubbing respond_to?

Add some tests with local methods
This commit is contained in:
ccocchi 2012-09-12 01:33:04 +02:00
parent 5ca0a44241
commit dc66b43638
3 changed files with 28 additions and 11 deletions

View File

@ -19,7 +19,6 @@ class DeepNestingTest < ActiveSupport::TestCase
@post = Post.new(42, 'I rock !') @post = Post.new(42, 'I rock !')
@user = User.new(1, 'foobar', 'male') @user = User.new(1, 'foobar', 'male')
@user.stub(:posts).and_return([@post]) @user.stub(:posts).and_return([@post])
@user.stub(:respond_to?).with(:each).and_return(false)
@context = Context.new @context = Context.new
@context.assigns['user'] = @user @context.assigns['user'] = @user

View File

@ -4,7 +4,6 @@ class TestJsonRenderer < ActiveSupport::TestCase
setup do setup do
@data = User.new(1, 'foobar', 'male') @data = User.new(1, 'foobar', 'male')
@data.stub(:respond_to?).with(:each).and_return(false)
@context = Context.new @context = Context.new
@context.assigns['data'] = @data @context.assigns['data'] = @data
@ -28,6 +27,13 @@ class TestJsonRenderer < ActiveSupport::TestCase
assert_equal %q([{}]), render_json_output assert_equal %q([{}]), render_json_output
end 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 test "render single object attributes" do
@template.source = { :id => :id, :name => :name } @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
@ -44,6 +50,12 @@ class TestJsonRenderer < ActiveSupport::TestCase
assert_equal %q({"author":{"name":"foobar"}}), render_json_output assert_equal %q({"author":{"name":"foobar"}}), render_json_output
end 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 test "render glued attributes from single object" do
@template.source = { :_glue0 => { :_data => :@data, :name => :name } } @template.source = { :_glue0 => { :_data => :@data, :name => :name } }
assert_equal %q({"name":"foobar"}), render_json_output assert_equal %q({"name":"foobar"}), render_json_output
@ -75,7 +87,7 @@ class TestJsonRenderer < ActiveSupport::TestCase
@template.source = { :name => [condition, proc] } @template.source = { :name => [condition, proc] }
assert_equal %q({}), render_json_output assert_equal %q({}), render_json_output
end end
test "node with context method call" do test "node with context method call" do
@context.stub(:respond_to?).with(:context_method).and_return(true) @context.stub(:respond_to?).with(:context_method).and_return(true)
@context.stub(:context_method).and_return('marty') @context.stub(:context_method).and_return('marty')
@ -86,7 +98,6 @@ class TestJsonRenderer < ActiveSupport::TestCase
test "partial should be evaluated at rendering time" do test "partial should be evaluated at rendering time" do
# Set assigns # Set assigns
@data.stub(:respond_to?).with(:empty?).and_return(false)
@context.assigns['user'] = @data @context.assigns['user'] = @data
# Stub Library#get # Stub Library#get
@ -114,17 +125,17 @@ class TestJsonRenderer < ActiveSupport::TestCase
assert_equal %q({"users":[]}), render_json_output assert_equal %q({"users":[]}), render_json_output
end end
test "render object with root node" do test "render object with root node" do
@template.root_name = :author @template.root_name = :author
@template.source = { :id => :id, :name => :name } @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 end
test "render object with root options set to false" do test "render object with root options set to false" do
RablRails.include_json_root = false RablRails.include_json_root = false
@template.root_name = :author @template.root_name = :author
@template.source = { :id => :id, :name => :name } @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
end end

View File

@ -32,12 +32,12 @@ end
class Context class Context
attr_writer :virtual_path attr_writer :virtual_path
def initialize def initialize
@_assigns = {} @_assigns = {}
@virtual_path = nil @virtual_path = nil
end end
def assigns def assigns
@_assigns @_assigns
end end
@ -47,4 +47,11 @@ class Context
end end
end end
User = Struct.new(:id, :name, :sex) class User
attr_accessor :id, :name, :sex
def initialize(id=nil, name=nil, sex=nil)
@id = id
@name = name
@sex = sex
end
end