In Rails, the serializer allows you to customize the data instead of having a default rendering.
In this article, we’ll look at a step-by-step guide to using serializers in your applications. The following is the implementation of Rspec in the Serializer.
1. Add serializer gem to Gemfile
gem 'active_model_serializers'
go back to terminal and run
bundle install
2. create a serializer file
bundle exec rails g serializer Customer
edit app/serializers/customer.rb add validation
class Customer < ActiveModel::Serializer
attributes :name, :email
end
In the example file above, the response in the client json will show the name and email attributes.
function The serializer file itself can be used to customize the response based on the front-end response requirements.
3. Install the factory robot
Add the Gem Factory bot to Gemfile in the :development, :test group
gem 'factory_bot_rails'
4. Configure Factory Bot
In connection with the previous tutorial in this tutorial, we still use the Customer.rb model.
then open terminal to create factory file in rspec folder by running command
rails g factory_bot:model customer
After running the above command, a file like this will be automatically created
after creating the spec/factories/customers.rb file
configure factory customer.rb file as below
FactoryBot.define do
factory :customer do |f|
f.name {Faker::Name.name}
f.email {Faker::Internet.email}
end
end
5. Configure RSpec with Serializer and Factory Bot
add folders and files like this spec/support/serializer_spec_helper.rb
then add the line of code like below
module SerializerSpecHelper
def serialize(obj, opts={})
serializer_class = opts.delete(:serializer_class) || "#{obj.class.name}Serializer".constantize
serializer = serializer_class.send(:new,obj)
adapter = ActiveModelSerializers::Adapter.create(serializer, opts)
adapter.to_json
end
end
Configure RSpec configuration with factory bot, add spec/support/factory_bot.rb file
then add the code below
RSpec.configure do |config|
config.include FactoryBot::Syntax::Methods
end
Add the code below to the spec/rails_helper.rb file, so that RSpec can detect files in the support/ and serializer_spec_helper.rb folders.
Dir [Rails.root.join('spec', 'support', '**', '*.rb'].sort.each { |f| require f}
RSpec.configure do |config| #sisipkan kode dibawah
config.include SerializerSpecHelper, type: :serializer
6. Configure Skenario Test RSpec and FactoryBot serializer
[ {
"name": "Persada Ersad",
"email": "ersad@doterb.com"
}
]
Testing RSpec for CustomerSerializer produces response data like the example case above.
Add the code below to the spec/serializers/customer_serializer_spec.rb folder and file
require 'rails_helper'
RSpec.describe CustomerSerializer, :type => :serializer do
customer = FactoryBot.create(:customer)
serializer = CustomerSerializer.new(customer)
describe 'attributes' do
it { expect(serializer).to include(:name, :email) }
it 'returns data should be contain name and email' do
expect(serializer).to include(
name: customer.name,
email: customer.email
)
end
end
end
Then go back to the terminal and type the command
rspec

We are trying to train if there are data errors in the CustomerSerializer response
in the app/serializers/customer_serializer.rb file remove the ‘:email’ code
class CustomerSerializer < ActiveModel::Serializer
attributes :name
end
Then go back to the terminal and type the command
rspec
the result will be a fail test as below:

Good luck and congratulations 😀
Tutorial created by: Doterb | Persada Ersad
PakarPBN
A Private Blog Network (PBN) is a collection of websites that are controlled by a single individual or organization and used primarily to build backlinks to a “money site” in order to influence its ranking in search engines such as Google. The core idea behind a PBN is based on the importance of backlinks in Google’s ranking algorithm. Since Google views backlinks as signals of authority and trust, some website owners attempt to artificially create these signals through a controlled network of sites.
In a typical PBN setup, the owner acquires expired or aged domains that already have existing authority, backlinks, and history. These domains are rebuilt with new content and hosted separately, often using different IP addresses, hosting providers, themes, and ownership details to make them appear unrelated. Within the content published on these sites, links are strategically placed that point to the main website the owner wants to rank higher. By doing this, the owner attempts to pass link equity (also known as “link juice”) from the PBN sites to the target website.
The purpose of a PBN is to give the impression that the target website is naturally earning links from multiple independent sources. If done effectively, this can temporarily improve keyword rankings, increase organic visibility, and drive more traffic from search results.
