Setup RSpec Ruby on Rails API Mode dengan Serializer & Factory Bot Bagian 2

Di Rails, serializer memungkinkan Anda untuk menyesuaikan data alih-alih memiliki render default.

Pada artikel ini, kita akan melihat panduan langkah demi langkah untuk menggunakan serializer di aplikasi Anda. Berikut dengan implementasi Rspec pada Serializer.

1. Tambahkan gem serializer pada Gemfile

gem 'active_model_serializers'

kembali ke terminal dan run 

 bundle install 

2. membuat sebuah file Serializer

 bundle exec rails g serializer Customer

edit app/serializers/customer.rb tambahkan validasi

 class Customer < ActiveModel::Serializer  
  attributes :name, :email
 end  

pada contoh file diatas response pada json customer akan menampilkan attributes name & email.

fungsi file serializer sendiri dapat digunakan untuk mengkustom response sesuai kebutuhan response pada front end.

3. Install Factory Bot

Tambahkan gem factory bot pada Gemfile pada group :development, :test

gem 'factory_bot_rails'

4. Konfigurasi factory bot 

Berhubungan dengan tutorial sebelumnya pada tutorial ini Kita masih menggunakan model Customer.rb.

lalu buka terminal unutk membuat file factory pada folder rspec dengan menjalankan command

rails g factory_bot:model customer

setelah menjalankan command diatas, otomatis akan terbuat file seperti ini

setelah file spec/factories/customers.rb terbuat

konfigurasi file factory customers.rb seperti dibawah

FactoryBot.define do 
  factory :customer do |f|
    f.name {Faker::Name.name}
    f.email {Faker::Internet.email}
   end
end 

5. Konfigurasi RSpec dengan Serializer & Factory Bot

tambahkan folder dan file seperti ini spec/support/serializer_spec_helper.rb

lalu tambahkan baris kode seperti dibawah

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 

Setup konfigurasi RSpec dengan factory bot, tambahkan file spec/support/factory_bot.rb

lalu tambahkan kode dibawah ini

RSpec.configure do |config|
 config.include FactoryBot::Syntax::Methods  
end 

Tambahkan kode dibawah pada file spec/rails_helper.rb, untuk RSpec bisa mendeteksi file pada folder support/ dan serializer_spec_helper.rb.

 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. Setup Skenario Test RSpec Serializer & FactoryBot

Contoh kasus front end membutuhkan response data seperti berikut:

[

{

"name": "Persada Ersad",

"email": "ersad@doterb.com"

}

]

Testing  RSpec untuk CustomerSerializer menghasilkan, response data seperti contoh kasus diatas.

Tambahkan kode dibawah pada folder dan file spec/serializers/customer_serializer_spec.rb

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 

Selanjutnya balik ke terminal dan ketikan command

rspec

Kita coba untuk mempraktekan jika ada kesalahan data pada response CustomerSerializer

pada file app/serializers/customer_serializer.rb hapus kode ‘:email’

class CustomerSerializer < ActiveModel::Serializer
  attributes :name
end

Selanjutnya balik ke terminal dan ketikan command

rspec

hasilnya akan ada failure test seperti dibawah ini:

Selamat mencoba & Cheers :D 

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.

Jasa Backlink

Download Anime Batch