這是我的課
class MyClass
def run
to_be_mocked("arg")
## etc
end
private
def to_be_mocked(arg)
# implementation
end
end
我的控制器,也就是我正在撰寫請求規范的物件,呼叫這個類。這是我的請求規格:
context "Some context" do
context "some sub context" do
before :each do
allow(MyClass). to receive(: to_be_mocked).with(account.url).and_return(false)
end
it "responds with a 200" do
do_request
expect(JSON.parse(response.body)["field"]).to eq true
expect(response.status).to eq 200
end
end
但是我的嘲笑失敗了MyClass does not implement: to_be_mocked
已經嘗試洗掉 private 關鍵字,但得到了相同的結果。
我在這里想念什么?
uj5u.com熱心網友回復:
你在模擬這個類,這就是你如何模擬你的“靜態”類級方法。例如,如果您的方法是def self.foo并且您通過呼叫它MyClass.foo,那么allow(MyClass)就是要走的路。
您的方法不是類級別的方法,而是實體方法。您可以通過首先創建一個而不是MyClass然后在該實體上呼叫該方法來呼叫它。您需要使用allow_any_instance_of該類的所有未來實體來模擬該方法:
allow_any_instance_of(MyClass).to receive(....)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/431887.html
下一篇:[7.1]在遷移中是什么意思
