test/model_representing_test.rb in roar-0.8.0 vs test/model_representing_test.rb in roar-0.8.1

- old
+ new

@@ -1,28 +1,31 @@ require 'test_helper' require 'roar/representer/feature/model_representing' class ModelRepresentingTest < MiniTest::Spec describe "ModelRepresenting" do - class ItemRepresenter < Roar::Representer::XML - include Roar::Representer::Feature::ModelRepresenting # TODO: move to abstract! + class ItemRepresenter + include Roar::Representer::XML + include Roar::Representer::Feature::ModelRepresenting self.representation_name= :item - representable_property :value + property :value end - class PositionRepresenter < Roar::Representer::XML - include Roar::Representer::Feature::ModelRepresenting # TODO: move to abstract! + class PositionRepresenter + include Roar::Representer::XML + include Roar::Representer::Feature::ModelRepresenting self.representation_name= :position - representable_property :id - representable_property :item, :as => ItemRepresenter + property :id + property :item, :as => ItemRepresenter end - class OrderRepresenter < Roar::Representer::XML - include Roar::Representer::Feature::ModelRepresenting # TODO: move to abstract! + class OrderRepresenter + include Roar::Representer::XML + include Roar::Representer::Feature::ModelRepresenting self.representation_name= :order - representable_property :id - representable_property :items, :as => [ItemRepresenter] + property :id + collection :items, :as => ItemRepresenter end describe "#definition_class" do it "returns ModelDefinition" do assert_equal Roar::Representer::Feature::ModelRepresenting::ModelDefinition, OrderRepresenter.send(:definition_class) @@ -31,37 +34,26 @@ end describe "#for_model" do it "copies represented model attributes, nothing more" do @o = Position.new("id" => 1, "item" => Item.new("value" => "Beer")) - @r = PositionRepresenter.for_model(@o) assert_kind_of PositionRepresenter, @r assert_equal 1, @r.id @i = @r.item assert_kind_of ItemRepresenter, @i assert_equal "Beer", @i.value end - it "copies the model to @represented" do + it "references the model in @represented" do @o = Position.new("id" => 1, "item" => @i = Item.new("value" => "Beer")) @r = PositionRepresenter.for_model(@o) assert_equal @o, @r.represented assert_equal @i, @r.item.represented end - - - it "works with Hyperlink attributes" do - @c = Class.new(ItemRepresenter) do - link :self do "http://self" end - end - - assert_equal({"value"=>"Beer", "links"=>[{"rel"=>:self, "href"=>"http://self"}]}, @c.for_model(Item.new("value" => "Beer")).to_attributes) - end - end describe "#serialize_model" do it "skips empty :item" do @o = Position.new("id" => 1) @@ -91,24 +83,30 @@ @r = OrderRepresenter.for_model(@o) OrderRepresenter.class_eval do include Roar::Representer::Feature::ActiveRecordMethods end + ItemRepresenter.class_eval do + include Roar::Representer::Feature::ActiveRecordMethods + end assert_equal({"id" => 1, "items_attributes" => [{"value" => "Beer"}]}, @r.to_nested_attributes) # DISCUSS: overwrite #to_attributes. end it "doesn't include :links" do @o = Order.new("id" => 1, "items" => [Item.new("value" => "Beer")]) OrderRepresenter.class_eval do include Roar::Representer::Feature::ActiveRecordMethods + include Roar::Representer::Feature::Hypermedia link :self do # "bla" end end ItemRepresenter.class_eval do + include Roar::Representer::Feature::ActiveRecordMethods + include Roar::Representer::Feature::Hypermedia link :self do end end @r = OrderRepresenter.for_model(@o)