Permalink
Browse files

Adding test suite for SMS module

  • Loading branch information...
1 parent ddc882d commit 29a0e14b88826375c984b6e37cd1d5df18b63326 @staskie staskie committed Dec 30, 2010
Showing with 61 additions and 0 deletions.
  1. +4 −0 test/helper.rb
  2. +57 −0 test/test_sms.rb
View
@@ -0,0 +1,4 @@
+require 'test/unit'
+require 'mocha'
+
+require File.join(File.dirname(__FILE__), '../lib/sms')
View
@@ -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.