test/responder_test.rb in roar-rails-0.0.14 vs test/responder_test.rb in roar-rails-0.0.15
- old
+ new
@@ -9,101 +9,11 @@
def singers
each
end
end
-module ObjectRepresenter
-end
-module ObjectsRepresenter
-end
-class RepresentsTest < MiniTest::Spec
- class SingersController
- end
-
- before do
- @controller = Class.new do
- include Roar::Rails::ControllerAdditions
- end.new
- end
-
- describe "representer_for" do
- describe "nothing configured" do
- before do
- @controller = class ::SingersController
- include Roar::Rails::ControllerAdditions
- self
- end.new
- end
-
- it "uses model class" do
- assert_equal SingerRepresenter, @controller.representer_for(:json, Singer.new)
- end
-
- it "uses plural controller name when collection" do
- assert_equal SingersRepresenter, @controller.representer_for(:json, [])
- end
- end
-
- describe "represents :json, Singer" do
- before do
- @controller = class ::WhateverController < ActionController::Base
- include Roar::Rails::ControllerAdditions
- represents :json, Object
- self
- end.new
- end
-
- it "uses defined class for item" do
- assert_equal ObjectRepresenter, @controller.representer_for(:json, Singer.new)
- end
-
- it "uses plural name when collection" do
- assert_equal ObjectsRepresenter, @controller.representer_for(:json, [])
- end
- end
-
-
- describe "represents :json, :entity => SingerRepresenter" do
- before do
- @controller = class ::FooController < ActionController::Base
- include Roar::Rails::ControllerAdditions
- represents :json, :entity => "ObjectRepresenter"
- self
- end.new
- end
-
- it "returns :entity representer name" do
- assert_equal "ObjectRepresenter", @controller.representer_for(:json, Singer.new)
- end
-
- it "doesn't infer collection representer" do
- assert_equal nil, @controller.representer_for(:json, [])
- end
- end
-
- describe "represents :json, :entity => SingerRepresenter, :collection => SingersRepresenter" do
- before do
- @controller = class ::BooController < ActionController::Base
- include Roar::Rails::ControllerAdditions
- represents :json, :entity => "ObjectRepresenter", :collection => "SingersRepresenter"
- self
- end.new
- end
-
- it "uses defined class for item" do
- assert_equal "ObjectRepresenter", @controller.representer_for(:json, Singer.new)
- end
-
- it "uses defined class when collection" do
- assert_equal "SingersRepresenter", @controller.representer_for(:json, [])
- end
- end
- end
-end
-
-
class ResponderTest < ActionController::TestCase
include Roar::Rails::TestCase
class BaseController < ActionController::Base
include Roar::Rails::ControllerAdditions
@@ -112,20 +22,31 @@
def execute
instance_exec &@block
end
end
- class UniqueRepresentsOptionsTest < ResponderTest
+ class UniqueRepresentsOptionsTest < MiniTest::Spec
class One < BaseController
represents :json, Object
end
class Two < BaseController
represents :json, Singer
end
- test "each subclass of a roar-augmented controller can represent different things" do
- assert_not_equal One.represents_options, Two.represents_options
+
+ it "each subclass of a roar-augmented controller can represent different things" do
+ One.represents_options.wont_equal Two.represents_options
end
+
+ it "does not share RepresenterComputer instances when inheriting" do
+ Class.new(One) do
+ represents :json, Singer
+ end.represents_options.wont_equal One.represents_options
+ end
+
+ it "inherits when subclass doesn't call ::represents" do
+ Class.new(One).represents_options.must_equal One.represents_options
+ end
end
class UnconfiguredControllerTest < ResponderTest
SingersRepresenter = ::SingersRepresenter
class SingersController < BaseController
@@ -137,11 +58,11 @@
get do
singer = Singer.new("Bumi")
respond_with singer
end
- assert_equal singer.to_json, @response.body
+ @response.body.must_equal singer.to_json
end
test "responder finds SingersRepresenter for collections by convention" do
get do
singers = [Singer.new("Bumi"), Singer.new("Bjork"), Singer.new("Sinead")]
@@ -207,11 +128,11 @@
get do
singer = Singer.new("Bumi")
respond_with singer
end
- assert_equal singer.to_json, @response.body
+ @response.body.must_equal singer.to_json
end
test "responder uses configured representer for collection" do
get do
singers = [Singer.new("Bumi"), Singer.new("Bjork"), Singer.new("Sinead")]
@@ -257,17 +178,17 @@
end
end
class PassingUserOptionsTest < ResponderTest
# FIXME: should be in generic roar-rails test.
- module SingerRepresenter
+ module DynamicSingerRepresenter
include Roar::Representer::JSON
property :name, :setter => lambda { |val, opts| self.name = "#{opts[:title]} #{val}" },
:getter => lambda { |opts| "#{opts[:title]} #{name}" }
end
class MusicianController < BaseController
- represents :json, :entity => SingerRepresenter, :collection => SingersRepresenter
+ represents :json, :entity => DynamicSingerRepresenter, :collection => SingersRepresenter
end
tests MusicianController
test "passes options to entity representer" do
@@ -279,10 +200,10 @@
@response.body.must_equal("{\"name\":\"Mr. Bumi\"}")
end
test "passes options to explicit collection representer" do
get do
- respond_with [Singer.new("Bumi"), Singer.new("Iggy")], :title => "Mr.", :represent_items_with => SingerRepresenter
+ respond_with [Singer.new("Bumi"), Singer.new("Iggy")], :title => "Mr.", :represent_items_with => DynamicSingerRepresenter
end
@response.body.must_equal("[{\"name\":\"Mr. Bumi\"},{\"name\":\"Mr. Iggy\"}]")
end