Reputation: 1319
In my Rails application I am testing a instance method that sends a SMS message out. It looks like this:
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
I'm having trouble figuring out how to test the twilio_sms.account.messages.create
part.
My spec looks something like this so far:
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
This obviously isn't working or I wouldn't be asking this question. What is the proper way to test that the create message was sent properly? Or am I approaching this the wrong way? Any help would be appreciated, thanks!
Upvotes: 2
Views: 1940
Reputation: 4639
Try this
describe Group do
describe 'instance methods' do
describe '.send_notifictions' do
it 'creates a new message' do
twilio_sms = double(:twilio_sms)
twilio_messages = double(:twilio_messages)
expect(Twilio::REST::Client).to receive(:new).with('mykey', 'mykey').and_return(twilio_sms)
expect(twilio_sms).to receive(:account).and_return(twilio_messages)
expect(twilio_messages).to receive(:messages).and_return(twilio_messages)
expect(twilio_messages).to receive(:create).with({:from => "+188888888", :to => "+15555555", :body => "Message from Company: New item uploaded to group"}).and_return(true)
group = Group.create(some_attributes: 'foo')
group.send_notifications
end
end
end
end
Upvotes: 2