---
title: "ปิด warning ของ rspec"
description: "$ RUBYOPT=W0 bundle exec rspec spec/ ..."
canonical: https://oozou.com/til/suppress-rspec-warnings-33
locale: th
date: 2020-10-13
topic: ruby
---

# ปิด warning ของ rspec

> $ RUBYOPT=W0 bundle exec rspec spec/ ...

Published: 2020-10-13 · Topic: [ruby](https://oozou.com/th/til-topics/ruby) ([markdown](https://oozou.com/th/til-topics/ruby.md))

```shell
$ RUBYOPT=W0 ./bin/rspec spec/
...

$ RUBYOPT=W0 bundle exec rspec spec/
...

$ RUBYOPT=W0 rspec
...

$ RUBYOPT=W0 ...
...
```

warning ของ RSpec อาจทำให้ผลลัพธ์ของ test รกและอ่าน log ยากขึ้น โดยเฉพาะในโปรเจกต์ Rails ขนาดใหญ่ นี่คือวิธีปิด warning อย่างได้ผลโดยยังคง workflow ที่สะอาดไว้

# ปิด warning แบบ global

ปิด warning ของทั้ง test suite ได้ด้วยการเพิ่มบรรทัดนี้ใน spec\_helper.rb หรือ rails\_helper.rb:

```
$VERBOSE = nil
```

Use Case: เหมาะสำหรับปิด warning ทั้งหมดอย่างรวดเร็ว แต่ก็อาจซ่อนข้อมูลสำคัญทั้งในแอปและ dependency

# ปิด warning แบบเฉพาะจุด

ครอบโค้ดที่สร้าง warning ด้วย block สำหรับปิด warning:

```
def suppress_warnings 
original_verbose = $VERBOSE 
$VERBOSE = nil 
yield 
ensure 
$VERBOSE = original_verbose 
end 

suppress_warnings do 
require 'some_gem_with_warnings' 
end
```

**Use Case**: มีประโยชน์เมื่อต้องการจำกัด warning ให้อยู่เฉพาะบาง dependency หรือบางส่วนของโค้ด

# ปิด warning ของ RSpec

RSpec มี configuration ในตัวสำหรับจัดการ warning ของมันเอง เพิ่มโค้ดนี้ใน spec\_helper.rb:

```
RSpec.configure do |config| 
config.warnings = false 
end
```

**Use Case**: ปิด warning ที่มาจาก RSpec โดยเฉพาะ แต่ยังเห็น warning ของ Ruby และ dependency อยู่

# Redirect warning ประเภท deprecation

สำหรับ deprecation warning ให้ redirect ไปเก็บในไฟล์ log เพื่อไว้ตรวจดูทีหลัง:

```
RSpec.configure do |config| 
config.deprecation_stream = File.open('log/rspec_deprecations.log', 'w') 
end
```

Use Case: ทำให้ผลลัพธ์ของ test สะอาด แต่ยังตามดู deprecation ได้อยู่

# ปิด warning เฉพาะบาง environment

ปิด warning เฉพาะใน environment ที่กำหนด เช่น CI:

```
$VERBOSE = nil if ENV['SUPPRESS_WARNINGS'] == 'true'
```

Use Case: ยังเห็น warning ระหว่างพัฒนาบนเครื่องตัวเอง แต่ซ่อนไว้ใน CI เพื่อให้ log สะอาดขึ้น

ปรับแนวทางเหล่านี้ตามความต้องการ แล้วผลลัพธ์ของ test จะสะอาดและนำไปใช้ต่อได้จริง ช่วยเพิ่ม productivity ทั้งในการพัฒนาและใน workflow ของ CI

---

Contact OOZOU: hello@oozou.com · +66 2544 0000 · https://oozou.com/th/contact

More for AI agents: https://oozou.com/llms.txt · every page has a markdown twin at its URL plus ".md"
