0
我需要編寫測試傳遞給方法的參數的示例。我該如何幹預使用預期參數調用方法的斷言?
選項1是返回接收到的參數作爲拍攝對象,驗證部分可以很簡潔:
subject do
color = nil
pen_double.should_receive(:color=) do |arg|
color = arg
end
color
end
context ...
it { should == 'red' }
end
context ...
it { should == 'green' }
end
此選項的問題是,「主題」部分是笨重。當我開始測試其他方法時(這裏有很多方法需要測試),它就成了一個大問題。
選項#2不使用「主題」。取而代之的是:
context ...
it { pen_double.should_receive(:color=).with('red') }
end
context ...
it { pen_double.should_receive(:color=).with('green') }
end
很明顯,'it'部分不夠乾燥。
有沒有辦法,我忽略了第三個選項?