mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 14:56:50 +00:00
add MiniTest and RSpec examples
This commit is contained in:
parent
56eaa61233
commit
79b84a10e3
25
README.md
25
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:
|
In order to test a Serializer, you can just call `.new` on it, passing the object to serialize:
|
||||||
|
|
||||||
|
### MiniTest
|
||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
post = Post.first
|
class TestPostSerializer < Minitest::Test
|
||||||
serializer = PostSerializer.new post
|
def setup
|
||||||
serializer.to_json
|
@serializer = PostSerializer.new Post.new(id: 123, title: 'some title', body: 'some text')
|
||||||
# => "{\"post\":{\"id\":1...}}"
|
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
|
## Caching
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user