我對我想做的似乎相當簡單的事情感到困惑。我有一個非常簡單的課程:
class User
def self.find_by_search(query)
query = query.to_s.strip
if query.upcase.start_with?("USER")
find(query)
else
find_by(name: "*#{query}*")
end
end
end
我很想測驗這個邏輯,所以我只需要斷言或者find使用find_by預期的引數呼叫。我嘗試了幾件事:
User.find_by_search("USER0000")
expect(ActiveRecord::Base).to receive(:find)
和
User.find_by_search("USER0000")
expect(User).to receive(:find) # didn't really think this would work, but figured I'd try anyway
兩者都導致
expected: 1 time with any arguments
received: 0 times with any arguments
我必須在這里做一些愚蠢的事情,但似乎無法弄清楚。
uj5u.com熱心網友回復:
expect().to receive()應該在我們執行操作之前定義。
expect(User).to receive(:find)
User.find_by_search("USER0000")
閱讀更多關于expect(...).to receive(...)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/460240.html
