test/responder_test.rb in roar-rails-0.0.12 vs test/responder_test.rb in roar-rails-0.0.13
- old
+ new
@@ -223,41 +223,99 @@
end
class ControllerWithDecoratorTest < ResponderTest
class SingerRepresentation < Representable::Decorator
include Roar::Representer::JSON
- include ::SingerRepresenter
+
+ property :name
end
+
class MusicianController < BaseController
represents :json, :entity => SingerRepresentation
end
tests MusicianController
- test "responder uses configured decorating representer" do
+ test "rendering uses decorating representer" do
get do
singer = Singer.new("Bumi")
respond_with singer
end
assert_equal "{\"name\":\"Bumi\"}", @response.body
end
+
+ test "parsing uses decorating representer" do # FIXME: move to controller_test.
+ created_singer = nil
+
+ put singer.to_json do
+ created_singer = consume!(Singer.new)
+ respond_with created_singer
+ end
+
+ created_singer.must_be_kind_of(Singer)
+ created_singer.name.must_equal "Bumi"
+ end
end
+ class PassingUserOptionsTest < ResponderTest
+ # FIXME: should be in generic roar-rails test.
+ module SingerRepresenter
+ 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
+ end
+ tests MusicianController
+
+ test "passes options to entity representer" do
+ get do
+ singer = Singer.new("Bumi")
+ respond_with singer, :title => "Mr."
+ end
+
+ @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
+ end
+
+ @response.body.must_equal("[{\"name\":\"Mr. Bumi\"},{\"name\":\"Mr. Iggy\"}]")
+ end
+
+ test "passes options in #consume!" do
+ created_singer = nil
+
+ put singer.to_json do
+ created_singer = consume!(Singer.new, :title => "Mr.")
+ respond_with created_singer
+ end
+
+ created_singer.must_be_kind_of(Singer)
+ created_singer.name.must_equal "Mr. Bumi"
+ end
+ end
+
+
+
def get(&block)
@controller.instance_eval do
@block = block
end
super :execute, :format => 'json'
end
- def put(&block)
+ def put(body="", &block)
@controller.instance_eval do
@block = block
end
- super :execute, :format => 'json'
+ super :execute, body, :format => 'json'
end
def singer(name="Bumi")
singer = Musician.new(name)
singer.extend SingerRepresenter