2
在我的Rails應用程序我測試發送SMS消息了實例方法。它看起來像這樣:Rspec的 - 測試方法接電話內的另一種方法
group.rb
def send_notifications
twilio_sms = Twilio::REST::Client.new('mykey', 'mykey')
begin
twilio_sms.account.messages.create(
from: "+188888888",
to: "+1#{phone_number}",
body: "Message from Company: New item uploaded to group"
)
rescue
puts 'Invalid number'
end
end
我無法搞清楚如何測試twilio_sms.account.messages.create
部分。
我的規格看起來像這樣至今:
group_spec.rb
describe Group do
context 'instance methods' do
describe '.send_notifictions' do
it 'calls the create method on the twilio messages object' do
twilio_messages = instance_double("Twilio::REST::Messages")
expect(twilio_messages).to receive(:create)
group = create(:group_with_notifications)
group.send_notifications
end
end
end
end
這顯然是不工作或我就不會問這個問題。測試創建消息是否正確發送的正確方法是什麼?還是我以錯誤的方式接近這個?任何幫助將不勝感激,謝謝!
真棒奏效,謝謝。是and_return部分實際上返回的對象或者他們只是希望他們回來? – cjxtr
我花了很長的時間來算出這個當我學習這種類型的測試中,'''and_return'''真的返回了對象的**改變狀態**。所以有必要把它們 – MilesStanfield