RSpec 期望 (Expectations)
rspec-expectations 用於定義預期的結果。
RSpec.describe Account do
it "has a balance of zero when first created" do
expect(Account.new.balance).to eq(Money.new(0))
end
end
基本結構
一個 RSpec 期望的基本結構是
expect(actual).to matcher(expected)
expect(actual).not_to matcher(expected)
注意:您也可以使用 expect(..).to_not
來代替 expect(..).not_to
。它們互為別名,所以您可以選擇讀起來比較順暢的方式使用。
範例
expect(5).to eq(5)
expect(5).not_to eq(4)
什麼是匹配器?
匹配器是任何回應以下方法的物件
matches?(actual)
failure_message
這些方法也是匹配器協議的一部分,但為可選:
does_not_match?(actual)
failure_message_when_negated
description
supports_block_expectations?
RSpec 提供許多內建的匹配器和用於編寫自訂匹配器的 DSL。
問題
rspec-expectations 的文件正在建構中。我們會隨著時間的推移增加 Cucumber 功能,並澄清現有的功能。如果您有想要新增的特定功能,發現現有的文件不完整或令人困惑,或者更好的是,希望自己編寫遺失的 Cucumber 功能,請 提交問題 或 提出提取請求。