test/responder_test.rb in roar-rails-0.0.6 vs test/responder_test.rb in roar-rails-0.0.7

- old
+ new

@@ -1,64 +1,128 @@ require 'test_helper' Singer = Struct.new(:name) +module SingersRepresenter + include Roar::Representer::JSON + + collection :singers, :extend => SingerRepresenter + def singers + each + end +end + + class ResponderTest < ActionController::TestCase include Roar::Rails::TestCase class SingersController < ActionController::Base include Roar::Rails::ControllerAdditions respond_to :json - def explicit_representer + def execute + instance_exec &@block + end + end + + def get(&block) + @controller.instance_eval do + @block = block + end + + super :execute, :format => 'json' + end + + + tests SingersController + + test ":with_representer is deprecated" do + assert_deprecated do + get do + singer = Musician.new("Bumi") + respond_with singer, :with_representer => SingerRepresenter + end + end + end + + + test "responder allows specifying representer" do # TODO: remove in 1.0. + get do singer = Musician.new("Bumi") respond_with singer, :with_representer => SingerRepresenter end + + assert_equal singer.to_json, @response.body + end - def implicit_representer + test "responder finds representer by convention" do + get do singer = Singer.new("Bumi") respond_with singer end + + assert_equal singer.to_json, @response.body + end + + - def collection_of_representers - singers = [Singer.new("Bumi"), Singer.new("Bjork"), Singer.new("Sinead")] - respond_with singers + test "responder works with collections" do # TODO: remove in 1.0. + assert_deprecated do + get do + singers = [Singer.new("Bumi"), Singer.new("Bjork"), Singer.new("Sinead")] + respond_with singers + end end + + assert_equal singers.map(&:to_hash).to_json, @response.body + end - def collection_of_custom_representers + test "custom responder works with collections" do # TODO: remove in 1.0. + get do singers = [Singer.new("Bumi"), Singer.new("Bjork"), Singer.new("Sinead")] respond_with singers, :with_representer => SingerAliasRepresenter end + + assert_equal singers.map {|s| s.extend(SingerAliasRepresenter).to_hash }.to_json, @response.body end - - tests SingersController - - test "responder allows specifying representer" do - get :explicit_representer, :format => 'json' - assert_equal singer.to_json, @response.body + + + + test "use passed :represent_with representer for single model" do + get do + singer = Musician.new("Bumi") + respond_with singer, :with_representer => SingerRepresenter + end + + assert_equal singer.extend(SingerRepresenter).to_json, @response.body end - - test "responder finds representer by convention" do - get :implicit_representer, :format => 'json' - assert_equal singer.to_json, @response.body + + test "use passed :represent_with representer for collection" do + get do + singers = [Singer.new("Bumi"), Singer.new("Bjork"), Singer.new("Sinead")] + respond_with singers, :represent_with => SingersRepresenter + end + + assert_equal({:singers => singers.collect {|s| s.extend(SingerRepresenter).to_hash }}.to_json, @response.body) end - - test "responder works with collections" do - get :collection_of_representers, :format => 'json' - assert_equal singers.map(&:to_hash).to_json, @response.body + + test "use passed :represent_items_with for collection items" do + get do + singers = [Singer.new("Bumi"), Singer.new("Bjork"), Singer.new("Sinead")] + respond_with singers, :represent_items_with => SingerRepresenter + end + + assert_equal(singers.collect {|s| s.extend(SingerRepresenter).to_hash }.to_json, @response.body) end + + + - test "custom responder works with collections" do - get :collection_of_custom_representers, :format => 'json' - assert_equal singers.map {|s| s.extend(SingerAliasRepresenter).to_hash }.to_json, @response.body - end - def singer(name="Bumi") singer = Musician.new(name) singer.extend SingerRepresenter end def singers [singer("Bumi"), singer("Bjork"), singer("Sinead")] end - end