From 56eaa61233f6b6e7c1a302bfb247c8385e7cf3c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20Schr=C3=B6der?= Date: Tue, 13 Aug 2013 15:17:16 +0200 Subject: [PATCH 1/2] add an example for how to test serializers easily off topic: please deactivate the WIKI if there is no content in it --- README.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/README.md b/README.md index 65a3b79f..48885816 100644 --- a/README.md +++ b/README.md @@ -676,6 +676,17 @@ for the current user, the advantage of this approach is that, by setting `serialization_scope` to `nil`, the `index` action no longer will need to make that query, only the `show` action will. +## Testing + +In order to test a Serializer, you can just call `.new` on it, passing the object to serialize: + +```ruby +post = Post.first +serializer = PostSerializer.new post +serializer.to_json +# => "{\"post\":{\"id\":1...}}" +``` + ## Caching To cache a serializer, call `cached` and define a `cache_key` method: From 79b84a10e33c5b2b5190616a07070fa3b4a2fc93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20Schro=CC=88der?= Date: Tue, 13 Aug 2013 20:16:11 +0200 Subject: [PATCH 2/2] add MiniTest and RSpec examples --- README.md | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 48885816..6ae54302 100644 --- a/README.md +++ b/README.md @@ -680,11 +680,28 @@ that query, only the `show` action will. In order to test a Serializer, you can just call `.new` on it, passing the object to serialize: +### MiniTest + ```ruby -post = Post.first -serializer = PostSerializer.new post -serializer.to_json -# => "{\"post\":{\"id\":1...}}" +class TestPostSerializer < Minitest::Test + def setup + @serializer = PostSerializer.new Post.new(id: 123, title: 'some title', body: 'some text') + end + + def test_special_json_for_api + assert_equal '{"post":{"id":123,"title":"some title","body":"some text"}}', @serializer.to_json + end +``` + +### RSpec + +```ruby +describe PostSerializer do + it "creates special JSON for the API" do + serializer = PostSerializer.new Post.new(id: 123, title: 'some title', body: 'some text') + expect(serializer.to_json).to eql('{"post":{"id":123,"title":"some title","body":"some text"}}') + end +end ``` ## Caching