← Back to list

RSpec — Rails อย่างง่าย! | บทที่ 2

สวัสดีครับทุกคน! ในบทนี้เราจะมาทำความเข้าใจเกี่ยวกับการใช้ RSpec และ Capybara สำหรับการทดสอบในโปรเจค Rails…

Bhuwadol Sriton · 2024-11-20 04:31 · 1 claps · 1.9 min read
#ruby #ruby-on-rails #capybara-testing #factorybot #devise-gem
Open on Medium ↗
Wiki topics: 🌐 · Web Development

RSpec — Rails อย่างง่าย! | บทที่ 2

สวัสดีครับทุกคน! ในบทนี้เราจะมาทำความเข้าใจเกี่ยวกับการใช้ RSpec และ Capybara สำหรับการทดสอบในโปรเจค Rails ซึ่งจะทำให้เราสามารถทดสอบระบบของเราได้แบบอัตโนมัติและตรวจสอบพฤติกรรมของแอปพลิเคชันได้อย่างมีประสิทธิภาพมากขึ้น โดยเน้นที่การทดสอบการเข้าถึงหน้า Admin โดยการใช้ RSpec และ Capybara ร่วมกัน

1. เพิ่ม Gem ที่จำเป็นใน Gemfile

ในขั้นตอนแรกเราต้องเพิ่ม Capybara และ Selenium WebDriver สำหรับการทดสอบระบบใน Gemfile เพื่อให้สามารถใช้งาน Capybara ได้อย่างสมบูรณ์:

group :test do
  # ใช้สำหรับการทดสอบระบบ
  gem "capybara"
  gem "selenium-webdriver"
end

หลังจากนั้นให้รันคำสั่ง bundle install เพื่อทำการติดตั้ง gems เหล่านี้

2. สร้าง Controller และ Route

หลังจากติดตั้ง dependencies เสร็จแล้ว เราจะสร้าง Controller สำหรับหน้า Home และ Admin::Home ด้วยคำสั่ง:

rails g controller home index
rails g controller admin::home index
rails db:migrate

จากนั้นให้กำหนดเส้นทางใน config/routes.rb สำหรับการเข้าถึงหน้า Admin และหน้า Home

Rails.application.routes.draw do
  namespace :admin do
    root to: "home#index"  # หน้าแรกของ admin จะเชื่อมโยงกับ action index ของ Admin::HomeController
  end
  get "home/index"
   # เส้นทางอื่นๆ ที่ต้องการ
end

3. สร้างไฟล์ทดสอบใน spec/system

ในขั้นตอนถัดไปเราจะสร้างไฟล์ทดสอบในโฟลเดอร์ spec/system และสร้างไฟล์ทดสอบชื่อว่า admin_access_spec.rb โดยมีเนื้อหาดังนี้:

require 'rails_helper'
describe "Admin Access", type: :system do
  context "when user is not logged in" do
    it "redirects to login page" do
      visit admin_root_path  # เข้าไปที่หน้า admin root
      expect(page).to have_content("Log in")  # คาดหวังให้หน้าเว็บแสดงคำว่า "Admin"
    end
  end
end

ในส่วนนี้ เราจะทดสอบว่าเมื่อผู้ใช้ไม่ได้ล็อกอินและพยายามเข้าถึงหน้า admin แล้วจะถูกเปลี่ยนเส้นทางไปยังหน้า login หรือไม่ จะ Failed

4. ติดตั้ง FactoryBot และ Devise

จากนั้น เราจะติดตั้ง FactoryBot และ Devise เพื่อใช้ในการสร้างข้อมูลผู้ใช้สำหรับการทดสอบและการจัดการการเข้าสู่ระบบ โดยการเพิ่มใน Gemfile:

group :development, :test do
  gem 'rspec-rails', '~> 7.0.0'
  gem 'factory_bot_rails'
end

gem "devise", "~> 4.9"

จากนั้นให้รันคำสั่ง:

bundle install
rails generate devise:install
rails generate devise user
rails db:migrate

4.1 สร้าง base_controller.rb in controllers/admin/base_controller.rb

เพื่อเพิ่มการ login ก่อน ใช้งาน admin ได้

module Admin
    class BaseController < ::ApplicationController
        before_action :authenticate_user!
    end
  end

4.2 นำ BaseController ไปใช้ ใน controllers/admin/home_controller.rb

class Admin::HomeController < Admin::BaseController
  def index
  end
end

5. การตั้งค่าการทดสอบ Devise และ FactoryBot

หลังจากนั้นเราต้องตั้งค่าการทดสอบสำหรับ Devise และ FactoryBot ในไฟล์ที่อยู่ในโฟลเดอร์ spec/support

5.1 ตั้งค่าการทดสอบสำหรับ Devise (spec/support/devise.rb)

RSpec.configure do |config|
  config.include Devise::Test::ControllerHelpers, type: :controller
  config.include Devise::Test::IntegrationHelpers, type: :system
  config.include Warden::Test::Helpers

  config.after do
      Warden.test_reset!
    end
end

5.2 ตั้งค่าการทดสอบสำหรับ FactoryBot (spec/support/factory_bot.rb)

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

5.3 สร้าง Factory สำหรับผู้ใช้ (spec/factories/users.rb)

# frozen_string_literal: true
FactoryBot.define do
  factory :user do
    sequence(:email) { |n| "person#{n}@example.com" }
    password { "password" }
    password_confirmation { "password" }
  end
end

5.3 uncomment line 26 in spec/rails_helper.rb file

Rails.root.glob('spec/support/**/*.rb').sort_by(&:to_s).each { |f| require f }

6. เขียน Test Case สำหรับเมื่อผู้ใช้ล็อกอิน

ตอนนี้เราจะเขียน test case สำหรับกรณีที่ผู้ใช้ได้ล็อกอินแล้วสามารถเข้าถึงหน้า admin ได้สำเร็จ

require 'rails_helper'
describe "Admin Access", type: :system do
  let(:user) { create(:user) }
  context "when user is not logged in" do
    it "redirects to login page" do
      visit admin_root_path  # เข้าไปที่หน้า admin root
      expect(page).to have_content("Log in")  # คาดหวังให้หน้าเว็บแสดงคำว่า "Log in"
    end
  end
  context "when user is logged in" do
    before do
      sign_in user  # ทำการล็อกอินผู้ใช้
    end
    it "allows access to admin page" do
      visit admin_root_path  # เข้าไปที่หน้า admin root
      expect(page).to have_content("Admin::Home#index")  # คาดหวังให้หน้าเว็บแสดงเนื้อหาของ admin page
    end
  end
end

7. การรันการทดสอบ

เมื่อเราเขียนการทดสอบเสร็จแล้ว สามารถรันการทดสอบทั้งหมดได้ด้วยคำสั่ง:

rspec

ถ้าไม่ได้ :

rails db:test:prepare

สรุป

ในบทนี้เราได้เรียนรู้วิธีการตั้งค่าและใช้งาน RSpec ร่วมกับ Capybara ในการทดสอบการเข้าถึงหน้า Admin ของแอปพลิเคชัน Rails โดยการใช้ Devise สำหรับการจัดการผู้ใช้งาน และ FactoryBot สำหรับการสร้างข้อมูลผู้ใช้ในการทดสอบ เราทำการเขียนและทดสอบหลายกรณี เช่น การล็อกอินและการเข้าถึงหน้า admin เพื่อให้แน่ใจว่าระบบของเราทำงานได้ถูกต้อง

หวังว่าบทนี้จะช่วยให้คุณสามารถเริ่มต้นการใช้งาน RSpec และ Capybara สำหรับทดสอบระบบใน Rails ได้อย่างมีประสิทธิภาพมากขึ้นครับ!

resoure :

[embed]GitHub — teamcapybara/capybara: Acceptance test framework for web applications Acceptance test framework for web applications. Contribute to teamcapybara/capybara development by creating an account…github.com

[embed]GitHub — thoughtbot/factory_bot_rails: Factory Bot ♥ Rails Factory Bot ♥ Rails. Contribute to thoughtbot/factory_bot_rails development by creating an account on GitHub.github.com

[embed]GitHub — heartcombo/devise: Flexible authentication solution for Rails with Warden. Flexible authentication solution for Rails with Warden. — heartcombo/devisegithub.com


메타데이터
post_id
b5fbe7dca600
slug
rspec-rails-อย่างง่าย-บทที่-2-b5fbe7dca600
url
https://medium.com/@ttthisriton29/rspec-rails-%E0%B8%AD%E0%B8%A2%E0%B9%88%E0%B8%B2%E0%B8%87%E0%B8%87%E0%B9%88%E0%B8%B2%E0%B8%A2-%E0%B8%9A%E0%B8%97%E0%B8%97%E0%B8%B5%E0%B9%88-2-b5fbe7dca600
canonical_url
https://medium.com/@ttthisriton29/rspec-rails-%E0%B8%AD%E0%B8%A2%E0%B9%88%E0%B8%B2%E0%B8%87%E0%B8%87%E0%B9%88%E0%B8%B2%E0%B8%A2-%E0%B8%9A%E0%B8%97%E0%B8%97%E0%B8%B5%E0%B9%88-2-b5fbe7dca600
author_url
https://medium.com/@ttthisriton29
status
ok
fetched_at
2026-08-11 21:02:09