- RSpec 教程
- RSpec - 首頁
- RSpec - 簡介
- RSpec - 基本語法
- RSpec -編寫規範
- RSpec - 匹配器
- RSpec - 測試替身
- RSpec - 存根
- RSpec - 鉤子
- RSpec - 標籤
- RSpec - 主題
- RSpec - 輔助方法
- RSpec - 元資料
- RSpec - 過濾
- RSpec - 期望
- RSpec 資源
- RSpec - 快速指南
- RSpec - 有用資源
- RSpec - 討論
RSpec -編寫規範
在本章中,我們將建立一個新的 Ruby 類,將其儲存在自己的檔案中,並建立一個單獨的規範檔案來測試此類。
首先,在我們的新類中,它被稱為StringAnalyzer。這是一個簡單的類,顧名思義,它分析字串。我們的類只有一個方法has_vowels?,正如其名稱所示,如果字串包含母音則返回 true,否則返回 false。以下是StringAnalyzer的實現:
class StringAnalyzer
def has_vowels?(str)
!!(str =~ /[aeio]+/i)
end
end
如果您按照 HelloWorld 部分的操作進行操作,則建立了一個名為 C:\rspec_tutorial\spec 的資料夾。
如果您有 hello_world.rb 檔案,請將其刪除,並將上面的 StringAnalyzer 程式碼儲存到 C:\rspec_tutorial\spec 資料夾中名為 string_analyzer.rb 的檔案中。
以下是我們的規範檔案用於測試 StringAnalyzer 的原始碼:
require 'string_analyzer'
describe StringAnalyzer do
context "With valid input" do
it "should detect when a string contains vowels" do
sa = StringAnalyzer.new
test_string = 'uuu'
expect(sa.has_vowels? test_string).to be true
end
it "should detect when a string doesn't contain vowels" do
sa = StringAnalyzer.new
test_string = 'bcdfg'
expect(sa.has_vowels? test_string).to be false
end
end
end
將其儲存在相同的 spec 目錄中,命名為 string_analyzer_test.rb。
在您的 cmd.exe 視窗中,cd 到 C:\rspec_tutorial 資料夾並執行此命令:dir spec
您應該看到以下內容:
C:\rspec_tutorial\spec 目錄
09/13/2015 08:22 AM <DIR> . 09/13/2015 08:22 AM <DIR> .. 09/12/2015 11:44 PM 81 string_analyzer.rb 09/12/2015 11:46 PM 451 string_analyzer_test.rb
現在我們將執行我們的測試,執行此命令:rspec spec
當您將資料夾的名稱傳遞給rspec時,它會執行資料夾內所有規範檔案。您應該看到此結果:
No examples found. Finished in 0 seconds (files took 0.068 seconds to load) 0 examples, 0 failures
發生這種情況的原因是,預設情況下,rspec 只執行名稱以“_spec.rb”結尾的檔案。將 string_analyzer_test.rb 重新命名為 string_analyzer_spec.rb。您可以透過執行以下命令輕鬆完成此操作:
ren spec\string_analyzer_test.rb string_analyzer_spec.rb
現在,再次執行rspec spec,您應該看到如下所示的輸出:
F.
Failures:
1) StringAnalyzer With valid input should detect when a string contains vowels
Failure/Error: expect(sa.has_vowels? test_string).to be true
expected true
got false
# ./spec/string_analyzer_spec.rb:9:in `block (3 levels) in <top (required)>'
Finished in 0.015 seconds (files took 0.12201 seconds to load)
2 examples, 1 failure
Failed examples:
rspec ./spec/string_analyzer_spec.rb:6 # StringAnalyzer With valid
input should detect when a string contains vowels
Do you see what just happened? Our spec failed because we have a bug in
StringAnalyzer. The bug is simple to fix, open up string_analyzer.rb
in a text editor and change this line:
!!(str =~ /[aeio]+/i)
to this:
!!(str =~ /[aeiou]+/i)
現在,儲存您剛剛在 string_analyizer.rb 中所做的更改,並再次執行 rspec spec 命令,您現在應該看到如下所示的輸出:
.. Finished in 0.002 seconds (files took 0.11401 seconds to load) 2 examples, 0 failures
恭喜,規範檔案中的示例(測試)現在通過了。我們修復了包含母音方法的正則表示式中的一個錯誤,但我們的測試遠未完成。
新增更多示例來測試包含母音方法的各種型別的輸入字串是有意義的。
下表顯示了一些可以新增到新示例(it 塊)中的排列。
| 輸入字串 | 描述 | 使用 has_vowels? 的預期結果 |
|---|---|---|
| ‘aaa’,‘eee’,‘iii’,‘o’ | 只有一個母音且沒有其他字母。 | true |
| ‘abcefg’ | “至少一個母音和一些子音” | true |
| ‘mnklp’ | 只有子音。 | false |
| ‘’ | 空字串(無字母) | false |
| ‘abcde55345&??’ | 母音、子音、數字和標點符號。 | true |
| ‘423432%%%^&’ | 只有數字和標點符號。 | false |
| ‘AEIOU’ | 只有大寫母音。 | true |
| ‘AeiOuuuA’ | 只有大寫和小寫母音。 | true |
| ‘AbCdEfghI’ | 大寫和小寫母音和子音。 | true |
| ‘BCDFG’ | 只有大寫子音。 | false |
| ‘ ‘ | 只有空格字元。 | false |
由您決定將哪些示例新增到您的規範檔案中。有很多條件需要測試,您需要確定哪個條件子集最重要以及最能測試您的程式碼。
rspec 命令提供了許多不同的選項,要檢視所有選項,請鍵入rspec -help。下表列出了最常用的選項並描述了它們的功能。
| 序號 | 選項/標誌和說明 |
|---|---|
| 1 | -I PATH 將 PATH 新增到rspec在查詢 Ruby 原始檔時使用的載入(require)路徑。 |
| 2 | -r, --require PATH 新增一個特定的原始檔,以便在您的規範檔案(s) 中需要。 |
| 3 | --fail-fast 使用此選項,rspec 將在第一個示例失敗後停止執行規範。預設情況下,rspec 會執行所有指定的規範檔案,無論有多少失敗。 |
| 4 | -f, --format FORMATTER 此選項允許您指定不同的輸出格式。有關輸出格式的更多詳細資訊,請參閱格式化程式部分。 |
| 5 | -o, --out FILE 此選項指示 rspec 將測試結果寫入輸出檔案 FILE,而不是寫入標準輸出。 |
| 6 | -c, --color 啟用 rspec 輸出中的顏色。成功的示例結果將以綠色文字顯示,失敗將以紅色文字顯示。 |
| 7 | -b, --backtrace 在 rspec 的輸出中顯示完整的錯誤回溯。 |
| 8 | -w, --warnings 在 rspec 的輸出中顯示 Ruby 警告。 |
| 9 | -P, --pattern PATTERN 載入並執行與模式 PATTERN 匹配的規範檔案。例如,如果您傳遞 -p “*.rb”,rspec 將執行所有 Ruby 檔案,而不僅僅是那些以“_spec.rb”結尾的檔案。 |
| 10 | -e, --example STRING 此選項指示 rspec 執行其描述中包含文字 STRING 的所有示例。 |
| 11 | -t, --tag TAG 使用此選項,rspec 將僅執行包含標籤 TAG 的示例。請注意,TAG 指定為 Ruby 符號。有關詳細資訊,請參閱 RSpec 標籤部分。 |