Day20 — Ruby on Rails 測試篇 — Cucumber (內文有範例教如何寫中文測試)

被端走的小菜
6 min readSep 24, 2020

--

前一篇介紹了 RSpec Gem,這次換介紹 Cucumber 這個 Gem

後續的文章會以此 repo 作為範例

介紹

Q1. 心中或許會有疑問說,已經有 RSpec 為何還需要 Cucumber 增加測試的複雜度?

Cucumber 更像是讓人類看得懂的語言,來描述要測試的事情,且支援多種語系寫法,像是可以用中文寫測試,讓 PM 與客戶看得懂,也能成為將來驗收的依據

Q2. 可以只寫 Cucumber 不寫 RSpec 嗎?

小孩紙才做選擇,我全都要,這就要問施主你的選擇惹 XD

如何安裝

放在 :test 中,可參考此 commit

參考資料: Cucumber Installation官方文件有提醒 Ruby on Rails 的安裝方式與 Ruby 安裝方式不同喔!!

# Gemfile
gem 'cucumber-rails', require: false
gem 'database_cleaner'
# 記得要 bundle

上述 bundle 完成後,可輸入 rails g -h 會看到以下資訊

# 也可輸入 rails g -h | grep -B 2 cucumberCucumber:
cucumber:install

接著再終端機輸入,可參考此 commit

rails generate cucumber:install# 也可縮寫成
rails g cucumber:install

寫個 Cucumber 中文測試範例

由於之前已經透過 scaffold 建立 User

以下示範寫個中文測試,可參考此 commit

# features/user.feature#language: zh-TW
@user
功能: 建立使用者
這是示範的範例
支援中英文寫法
場景: 能建立使用者
假如 一開始沒有使用者
假如 有一位使用者的姓名是"小菜"
而且 該使用者的信箱是"river@riverye.com"
而且 該使用者的電話與地址資訊如下:
|phone |address |
|0987654321 |台北市某處 |
當 使用者被建立時
那麼 使用者會有1位
而且 使用者的資訊會是正確的:
|name |email |phone |address |
|小菜 |river@riverye.com |0987654321 |台北市某處 |

空的測試 (先寫規格,還沒準備答案)

如何執行

終端機輸入

# 跑全部檔案
cucumber
bundle exec cucumber
# 跑指定 tag
bundle exec cucumber -t @user
# 跑指定資料夾
bundle exec cucumber features
# 跑指定檔案
bundle exec cucumber features/user.feature

更多操作範例輸入

cucumber -hUsage: cucumber [options] [ [FILE|DIR|URL][:LINE[:LINE]*] ]+Examples:
cucumber examples/i18n/en/features
cucumber @rerun.txt (See --format rerun)
cucumber examples/i18n/it/features/somma.feature:6:98:113
cucumber -s -i http://rubyurl.com/eeCl

準備的測試答案

# features/step_definitions/user_step.rb假如('一開始沒有使用者') do
expect(User.count).to eq(0)
end
假如('有一位使用者的姓名是{string}') do |name|
@user = {}
@user.merge!(name: name)
end
假如('該使用者的信箱是{string}') do |email|
@user.merge!(email: email)
end
假如('該使用者的電話與地址資訊如下:') do |table|
table.hashes.each do |hash|
phone = hash['phone']
address = hash['address']
@user.merge!(phone: phone, address: address)
end
end
當('使用者被建立時') do
@user = User.create(@user)
end
那麼('使用者會有{int}位') do |count|
expect(User.count).to eq(count)
end
假如('使用者的資訊會是正確的:') do |table|
table.hashes.each do |hash|
hash.each do |key, value|
expect(@user[key]).to eq(value)
end
end
end

執行結果

如何讓 CI 也能跑 Cucumber

將原本 CI 設定檔新增以下,可參考此 commit

bundle exec cucumber

官方網站

https://cucumber.io/docs/cucumber/

參考資料

Cucumber 小黃瓜驗收測試工具 — ihower

小結

想不到測試可以寫中文吧 XD
有沒有覺得很親切? (噁心
瞬間人類都能看懂這份規格
且每一個測試都能重複使用
將來接手的工程師要改 code 時
也能看到原本的規格是如何制定
不用再通靈啦 (欣慰

鐵人賽文章連結:https://ithelp.ithome.com.tw/articles/10243344
medium 文章連結:https://link.medium.com/uP1cmw5R29
本文同步發布於 小菜的 Blog https://riverye.com/

備註:之後文章修改更新,以個人部落格為主

--

--

被端走的小菜
被端走的小菜

Written by 被端走的小菜

大家好,我是被端走的小菜。以個人部落格更新為主:https://riverye.com/

No responses yet