Last active
December 26, 2015 11:09
-
-
Save Supro/7141290 to your computer and use it in GitHub Desktop.
ActiveModelSerializers example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### spec/serializers/building_spec.rb | |
require "spec_helper" | |
describe BuildingSerializer do | |
include Rails.application.routes.url_helpers | |
let(:room) { create :room } | |
let(:building) { room.building } | |
let(:hash) { BuildingSerializer.new(building).serializable_hash } | |
let(:links_hash) { { rooms: { href: building_rooms_url(building) } } } | |
let(:rooms) { building.rooms.map{|room| RoomEmbedSerializer.new(room).serializable_hash } } | |
subject { hash } | |
its([:id]) { should eql building.id } | |
its([:name]) { should eql building.name } | |
its([:address]) { should eql building.address } | |
its([:floors_count]) { should eql building.floors_count } | |
its([:description]) { should eql building.description } | |
its([:_links]) { should eql links_hash } | |
its([:rooms]){ should be_nil } | |
context "expand: true" do | |
let(:hash) { BuildingSerializer.new(building, expand: true).serializable_hash } | |
its([:rooms]){ should eql rooms } | |
end | |
end | |
### spec/serializers/rooms_embed_spec.rb | |
require "spec_helper" | |
describe RoomEmbedSerializer do | |
let(:room) { create :room } | |
let(:hash) { RoomEmbedSerializer.new(room).serializable_hash } | |
subject { hash } | |
its([:id]) { should eql room.id } | |
its([:area]) { should eql room.area } | |
end | |
### spec/serializers/rooms_spec.rb | |
require "spec_helper" | |
describe RoomSerializer do | |
let(:room) { create :room } | |
let(:hash) { RoomSerializer.new(room).serializable_hash } | |
subject { hash } | |
its([:id]) { should eql room.id } | |
its([:area]) { should eql room.area } | |
its([:furnish]) { should eql room.furnish } | |
its([:building_id]) { should eql room.building_id } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment