configure API mode Rspec Ruby On Rails bagian 1

RSpec is a unit testing framework for the Ruby programming language. RSpec differs from traditional xUnit frameworks such as JUnit because RSpec is a behavior-based development tool. This means that tests written in RSpec focus on the “behavior” of the application under test.

RSpec is a DSL for creating executable examples of how code should behave, organized into groups. He uses the words “describe” and “that” so we can express concepts like conversation:

"Describe an account when it is first opened."
"It has a balance of zero."

1. configure and install ror and rspec

Create a new application with the following command

rails _7.0.1_ new sample_unit_test --api -d postgresql

go to the directory we created

 cd sample_unit_test 

open the Gemfile and add the ‘rspec-rails’ gem

go back to terminal and run

 bundle install 

create a database with run

 bundle exec rake db:create 

configure rspec on rails with the following command

bundle exec rails generate rspec:install  

2. Create a client model and raw features whose functionality we will test

 bundle exec rails g scaffold customer name:string phone:bigint email:string address:text
bundle exec rake db:migrate

edit app/models/customer.rb add validation

 class Customer < ApplicationRecord  
  validates :name, :email, presence: true  
 end  

3. update rspec file
When we run the scaffold ror command, it will also automatically create a test file

Image of the generated scaffolding

To test the customer controller, edit the file in spec/requests/customers_spec.rb

Some of the content was system generated by ROR but it is not yet perfect, there are several improvements to parameters and assertions. We need to study the existing code from index show create update and delete and we also need to understand assertions and matchers.
default isi file dari customer test request

Because the content is still empty, we need to fill in the correct attributes and use the “fake” gem to help match the data.

let(:valid_attributes) do  
   { name: Faker::Name.name,  
    phone: Faker::Number.number(digits: 12),  
    email: Faker::Internet.email,  
    address: Faker::Address.street_address }  
  end  
   
  let(:invalid_attributes) do  
   { name: nil,  
    phone: nil,  
    email: Faker::Internet.email,  
    address: Faker::Address.street_address }  
  end  
   
  let(:valid_headers) do  
   {}  
  end 

valid headers are left empty because this tutorial does not use security tokens

after adding valid_attributes and invalid_attributes try testing with the following command

bundle exec rspec spec/requests/customers_spec.rb 
Image after updating valid and invalid attributes

In the error description there are two processes waiting because there is a time skip in the update section
reopen the following file

spec/requests/customers_spec.rb 

Updated part of Gambar’s default code

The skipped section must be replaced according to the description of the new attribute, i.e. the data content. new_attributes To replace the old data, the content looks like the code below

    let(:new_name) { Faker::Name.name }  
    let(:new_attributes) do  
     { name: new_name,  
      phone: Faker::Number.number(digits: 12),  
      email: Faker::Internet.email,  
      address: Faker::Address.street_address }  
    end 
For the “assertions” section, we fill it by checking that the name data after updating is the same as the name on new_attributes

    it 'updates the requested customer' do  
     customer = Customer.create! valid_attributes  
     patch customer_url(customer),  
        params: { customer: new_attributes }, headers: valid_headers, as: :json  
     customer.reload  
     expect(customer.name).to eq(new_name)  
    end 
Check the test file again with the following command

bundle exec rspec spec/requests/customers_spec.rb 
Image of passed test results without failure

Test the customer model update spec/models/customer_spec.rb as follows, then run the tests

 require 'rails_helper'  
 RSpec.describe Customer, type: :model do  
  it 'is not valid without a name' do  
   customer = Customer.new(name: nil)  
   expect(customer).to_not be_valid  
  end  
  it 'is not valid without a email' do  
   customer = Customer.new(email: nil)  
   expect(customer).to_not be_valid  
  end  
 end 

testing for client models here is just a validation, in fact there can be a lot depending on the content of the model

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