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