Skip to content

Commit

Permalink
Adding test suite for SMS module
Browse files Browse the repository at this point in the history
  • Loading branch information
staskie committed Dec 30, 2010
1 parent ddc882d commit 29a0e14
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
4 changes: 4 additions & 0 deletions test/helper.rb
@@ -0,0 +1,4 @@
require 'test/unit'
require 'mocha'

require File.join(File.dirname(__FILE__), '../lib/sms')
57 changes: 57 additions & 0 deletions test/test_sms.rb
@@ -0,0 +1,57 @@
require 'helper'

class TestSms < Test::Unit::TestCase
SUCCESS_CODE = 201
ERROR_CODE = -1
FROM_NUMBER = "502938232"
TO_NUMBER = "948398434"
MESSAGE = "Text message"

def test_successful_message
response = mock()
Twilio::RestAccount.any_instance.stubs(:request).returns(response)
response.expects(:code).returns(SUCCESS_CODE)
assert_equal(true, SMS.text(MESSAGE, :from => FROM_NUMBER, :to => TO_NUMBER))
end

def test_unsuccessful_message
response = mock()
Twilio::RestAccount.any_instance.stubs(:request).returns(response)
response.expects(:code).returns(ERROR_CODE)
assert_equal(false, SMS.text(MESSAGE, :from => FROM_NUMBER, :to => TO_NUMBER))
end

def test_unsuccessful_message_with_exception
response = stub(:code => ERROR_CODE, :body => stub(:match => []))
Twilio::RestAccount.any_instance.stubs(:request).returns(response)
assert_raise TypeError do
SMS.text!(MESSAGE, :from => FROM_NUMBER, :to => TO_NUMBER)
end
end

def test_sending_sms_with_env_set
ENV['TWILIO_ID'] = "twilio_id"
ENV['TWILIO_SECRET'] = "twilio_secret"
link = "/2008-08-01/Accounts/#{ENV['TWILIO_ID']}/SMS/Messages"

twilio = mock()
twilio.expects(:request).with(link, "POST", {'To' => TO_NUMBER, 'From' => FROM_NUMBER, 'Body' => MESSAGE})
.returns(mock(:code => SUCCESS_CODE))

Twilio::RestAccount.expects(:new).with("twilio_id", "twilio_secret").returns(twilio)
assert_equal(true, SMS.text(MESSAGE, :from => FROM_NUMBER, :to => TO_NUMBER))
end

def test_sending_sms_without_env_set
SMS.twilio_id = "twilio_id"
SMS.twilio_secret = "twilio_secret"
link = "/2008-08-01/Accounts/#{SMS.twilio_id}/SMS/Messages"

twilio = mock()
twilio.expects(:request).with(link, "POST", {'To' => TO_NUMBER, 'From' => FROM_NUMBER, 'Body' => MESSAGE})
.returns(mock(:code => SUCCESS_CODE))

Twilio::RestAccount.expects(:new).with("twilio_id", "twilio_secret").returns(twilio)
assert_equal(true, SMS.text(MESSAGE, :from => FROM_NUMBER, :to => TO_NUMBER))
end
end

0 comments on commit 29a0e14

Please sign in to comment.