test/json_representer_test.rb in roar-0.9.2 vs test/json_representer_test.rb in roar-0.10.0
- old
+ new
@@ -28,10 +28,14 @@
end
it "is aliased by #serialize" do
assert_equal '{"id":1}', @order.serialize
end
+
+ it "accepts :include and :except" do
+ assert_equal '{}', @order.to_json(:except => [:id])
+ end
end
describe "#from_json" do
it "returns the deserialized model" do
@order.from_json('{"id":1}')
@@ -44,10 +48,19 @@
end
it "works with a nil document" do
assert @order.from_json(nil)
end
+
+ it "works with an empty document" do
+ assert @order.from_json('')
+ end
+
+ it "accepts :include and :except" do
+ @order.from_json('{"id":1}', :except => [:id])
+ assert_equal nil, @order.id
+ end
end
describe "JSON.from_json" do
it "is aliased by #deserialize" do
@order = Order.deserialize('{"id":1}')
@@ -55,23 +68,32 @@
end
end
end
end
+# test the generic roar+json HyperlinkRepresenter
class JsonHyperlinkRepresenterTest
describe "API" do
before do
- @l = Roar::Representer::JSON::Hyperlink.from_json({:rel => :self, :href => "http://roar.apotomo.de"}.to_json)
+ @link = Roar::Representer::Feature::Hypermedia::Hyperlink.new.extend(Roar::Representer::JSON::HyperlinkRepresenter).from_json({:rel => :self, :href => "http://roar.apotomo.de", :media => "web"}.to_json)
end
it "responds to #rel" do
- assert_equal "self", @l.rel
+ assert_equal "self", @link.rel
end
it "responds to #href" do
- assert_equal "http://roar.apotomo.de", @l.href
+ assert_equal "http://roar.apotomo.de", @link.href
end
+
+ it "responds to #media" do
+ assert_equal "web", @link.media
+ end
+
+ it "responds to #to_json" do
+ assert_equal "{\"rel\":\"self\",\"href\":\"http://roar.apotomo.de\",\"media\":\"web\"}", @link.to_json
+ end
end
end
class JsonHypermediaTest
describe "Hypermedia API" do
@@ -103,8 +125,19 @@
end
it "renders link: correctly in JSON" do
assert_equal "{\"id\":1,\"links\":[{\"rel\":\"self\",\"href\":\"http://self\"},{\"rel\":\"next\",\"href\":\"http://next/1\"}]}", @c.new(:id => 1).to_json
end
+
+ it "doesn't render links when empty" do
+ assert_equal("{\"links\":[]}", Class.new do
+ include Roar::Representer::JSON
+ include Roar::Representer::Feature::Hypermedia
+
+ link :self do nil end
+ link :next do false end
+ end.new.to_json)
+ end
+
end
end