[英]Writing steps in rspec-rails?
我剛剛開始測試,我想知道如何為 RSpec 規范編寫步驟,這樣我就可以重用很多功能,例如登錄等。
通常測試應該彼此隔離; 如果你的很多測試都需要做同樣的事情,這表明他們在重復一些工作。 但有時這是不可避免的——例如,您經常需要有一個方便的登錄用戶來測試經過身份驗證的東西。
特別是在 Ruby 測試的情況下,很有可能有人已經編寫了一個庫來解決您想要的特定問題。 例如,在正確測試操作之前需要一些數據是很常見的——這就是factory_girl存在的原因。
如果您想進行行為驅動的集成測試以遍歷用戶實際執行的所有步驟,則應改用Cucumber 。
如果你想在不同的地方重用方法,你可以把共享代碼放在spec/support
中:
# spec/support/consumable_helper.rb
module ConsumableHelper
def consume(consumable)
calories = consumable.om_nom_nom
end
end
RSpec.configure do |config|
config.include ConsumableHelper
end
如果您想在多個區域測試相同的行為,請使用shared_examples_for
和it_behaves_like
:
shared_examples_for "a Consumable" do
it "should be delicious" do
subject.should be_delicious
end
it "should provide nutrition" do
subject.calories.should > 0
end
end
describe Fruit do
it_behaves_like "a Consumable"
end
describe Meat do
it_behaves_like "a Consumable"
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.