是否有可能做這樣的事情?RSpec之前在幫手
module MyHelper
before (:each) do
allow(Class).to receive(:method).and_return(true)
end
end
然後在我的測試中,我可以這樣做:
RSpec.describe 'My cool test' do
include MyHelper
it 'Tests a Class Method' do
expect { Class.method }.to eq true
end
end
編輯:這將產生以下錯誤:
undefined method `before' for MyHelper:Module (NoMethodError)
基本上我有一個情況許多測試做了不同的事情,但一個共同的模型跨越了他們之後的反應_commit最終總是調用一個與API對話的方法。我不希望全球允許Class
收到:method
,有時候,我需要自己定義它以適應特殊情況......但我不想重複我的allow/receive/and_return,而是將它包裝在一個共同的幫助器中...
完美的解決方案!:) –
這指甲!謝謝@Stefan :) – Nick