假設我有一個實例方法,可以執行許多不同的事情,我需要測試它們,比如store#process_order
。我想測試它發送給客戶的電子郵件,在訂單表中添加一個條目,向信用卡收取費用等。在rspec中設置此值的最佳方法是什麼?目前,我使用rspec和工廠女孩我做這樣的事情:在rspec中用多個「it」塊設置一個測試
describe Store do
describe "#process_order" do
before do
@store = Factory(:store)
@order = Factory(:order)
# call the process method
@store.process_order(@order)
end
it 'sends customer an email' do
...
end
it 'inserts order to db' do
...
end
it 'charges credit card' do
...
end
end
end
但它感覺真的很乏味。這真的是寫一個規範的正確方法,我需要確保做幾件不同的事情嗎?
注:我對這個設計是否優秀並不感興趣。這只是我爲彌補我的問題而編寫的一個例子 - 如何編寫這些類型的規格。
如果它做了幾件不同的事情,你應該做幾個不同的測試。 DRY不是一個規則! – Eric