Permalink
Browse files

Add overrides to make sure no sms messages are actually send during t…

…esting and add assert_sms_messages/assert_no_sms_messages assertions.
  • Loading branch information...
1 parent 432fd0d commit af5355bbb280be31b5b3632a6b74aa56719d336b @alloy alloy committed Aug 3, 2010
Showing with 242 additions and 97 deletions.
  1. +2 −0 Rakefile
  2. +76 −0 lib/mollie/sms/test_helper.rb
  3. +71 −0 spec/functional_sms_deliver_spec.rb
  4. +7 −46 spec/sms_spec.rb
  5. +5 −51 spec/spec_helper.rb
  6. +81 −0 spec/test_helper_spec.rb
View
@@ -1,6 +1,8 @@
desc "Run the specs"
task :spec do
sh "ruby ./spec/sms_spec.rb"
+ sh "ruby ./spec/test_helper_spec.rb"
+ sh "ruby ./spec/functional_sms_deliver_spec.rb"
end
task :default => :spec
@@ -0,0 +1,76 @@
+module Mollie
+ class SMS
+ def self.deliveries
+ @deliveries ||= []
+ end
+
+ def self.reset!
+ @deliveries = []
+ success!
+ end
+
+ def self.success!
+ http_response = Net::HTTPOK.new('1.1', '200', 'OK')
+ http_response.add_field('Content-type', 'application/xml')
+ def http_response.read_body; TestHelper::SUCCESS_BODY; end
+ @stubbed_response = Mollie::SMS::Response.new(http_response)
+ end
+
+ def self.gateway_failure!
+ http_response = Net::HTTPOK.new('1.1', '200', 'OK')
+ http_response.add_field('Content-type', 'application/xml')
+ def http_response.read_body; TestHelper::FAILURE_BODY; end
+ @stubbed_response = Mollie::SMS::Response.new(http_response)
+ end
+
+ def self.http_failure!
+ @stubbed_response = Mollie::SMS::Response.new(Net::HTTPBadRequest.new('1.1', '400', 'Bad request'))
+ end
+
+ def self.stubbed_response
+ @stubbed_response || success!
+ end
+
+ def deliver
+ validate_params!
+ self.class.deliveries << self
+ self.class.stubbed_response
+ end
+
+ module TestHelper
+ SUCCESS_BODY = %{
+<?xml version="1.0" ?>
+<response>
+ <item type="sms">
+ <recipients>1</recipients>
+ <success>true</success>
+ <resultcode>10</resultcode>
+ <resultmessage>Message successfully sent.</resultmessage>
+ </item>
+</response>}
+
+ FAILURE_BODY = %{
+<?xml version="1.0"?>
+<response>
+ <item type="sms">
+ <recipients>1</recipients>
+ <success>false</success>
+ <resultcode>20</resultcode>
+ <resultmessage>No username given.</resultmessage>
+ </item>
+</response>}
+
+ def assert_sms_messages(number_of_messages)
+ before = Mollie::SMS.deliveries.length
+ yield
+ diff = Mollie::SMS.deliveries.length - before
+ assert(diff == number_of_messages, "expected `#{number_of_messages}' SMS messages to be sent, actually sent `#{diff}'")
+ end
+
+ def assert_no_sms_messages
+ assert_sms_messages(0) { yield }
+ end
+ end
+ end
+end
+
@@ -0,0 +1,71 @@
+require File.expand_path("../spec_helper", __FILE__)
+
+module Net
+ class HTTP
+ class << self
+ attr_accessor :posted, :stubbed_response
+
+ def reset!
+ @posted = nil
+ @stubbed_response = nil
+ end
+ end
+
+ def host
+ @address
+ end
+
+ def start
+ yield self
+ end
+
+ def request(request)
+ self.class.posted = [self, request]
+ self.class.stubbed_response
+ end
+ end
+end
+
+SUCCESS_BODY = %{
+<?xml version="1.0" ?>
+<response>
+ <item type="sms">
+ <recipients>1</recipients>
+ <success>true</success>
+ <resultcode>10</resultcode>
+ <resultmessage>Message successfully sent.</resultmessage>
+ </item>
+</response>}
+
+describe "When sending a Mollie::SMS message" do
+ before do
+ @sms = Mollie::SMS.new
+ @sms.telephone_number = '+31612345678'
+ @sms.body = "The stars tell me you will have chicken noodle soup for breakfast."
+ end
+
+ after do
+ Net::HTTP.reset!
+ end
+
+ it "posts the post body to the gateway" do
+ @sms.stubs(:params).returns('a key' => 'a value')
+ @sms.stubs(:validate_params!)
+ @sms.deliver
+
+ request, post = Net::HTTP.posted
+ request.should.use_ssl
+ request.host.should == Mollie::SMS::GATEWAY_URI.host
+ request.port.should == Mollie::SMS::GATEWAY_URI.port
+ post.path.should == Mollie::SMS::GATEWAY_URI.path
+ post.body.should == "a%20key=a%20value"
+ end
+
+ it "returns a Mollie::SMS::Response object, with the Net::HTTP response" do
+ Net::HTTP.stubbed_response = Net::HTTPOK.new('1.1', '200', 'OK')
+ Net::HTTP.stubbed_response.stubs(:read_body).returns(SUCCESS_BODY)
+ response = @sms.deliver!
+ response.should.be.instance_of Mollie::SMS::Response
+ response.http_response.should == Net::HTTP.stubbed_response
+ end
+end
View
@@ -1,9 +1,5 @@
require File.expand_path("../spec_helper", __FILE__)
-require "mollie/sms"
-
-Mollie::SMS.username = 'AstroRadio'
-Mollie::SMS.password = 'secret'
-Mollie::SMS.originator = 'Astro INC'
+require "mollie/sms/test_helper"
describe "Mollie::SMS" do
it "holds the gateway uri" do
@@ -108,61 +104,30 @@
@sms.body = "The stars tell me you will have chicken noodle soup for breakfast."
end
- after do
- Net::HTTP.reset!
- end
-
- it "posts the post body to the gateway" do
- @sms.stubs(:params).returns('a key' => 'a value')
- @sms.stubs(:validate_params!)
- @sms.deliver
-
- request, post = Net::HTTP.posted
- request.should.use_ssl
- request.host.should == Mollie::SMS::GATEWAY_URI.host
- request.port.should == Mollie::SMS::GATEWAY_URI.port
- post.path.should == Mollie::SMS::GATEWAY_URI.path
- post.body.should == "a%20key=a%20value"
- end
-
- it "returns a Mollie::SMS::Response object, with the Net::HTTP response" do
- Net::HTTP.stubbed_response = Net::HTTPOK.new('1.1', '200', 'OK')
- Net::HTTP.stubbed_response.stubs(:read_body).returns(SUCCESS_BODY)
- response = @sms.deliver!
- response.should.be.instance_of Mollie::SMS::Response
- response.http_response.should == Net::HTTP.stubbed_response
- end
-
it "raises a Mollie::SMS::DeliveryFailure exception when sending, through the #deliver! method, fails" do
- Net::HTTP.stubbed_response = Net::HTTPOK.new('1.1', '200', 'OK')
- Net::HTTP.stubbed_response.stubs(:read_body).returns(FAILURE_BODY)
+ Mollie::SMS.gateway_failure!
exception = nil
begin
@sms.deliver!
rescue Mollie::SMS::DeliveryFailure => exception
end
- exception.sms.should == @sms
- exception.response.http_response.should == Net::HTTP.stubbed_response
exception.message.should == "(No username given.) #{@sms.to_s}"
end
end
describe "A Mollie::SMS::Response instance, for a succeeded request" do
before do
- @http_response = Net::HTTPOK.new('1.1', '200', 'OK')
- @http_response.stubs(:read_body).returns(SUCCESS_BODY)
- @http_response.add_field('Content-type', 'application/xml')
- @response = Mollie::SMS::Response.new(@http_response)
+ @response = Mollie::SMS.success!
end
it "returns the Net::HTTP response object" do
- @response.http_response.should == @http_response
+ @response.http_response.should.is_a?(Net::HTTPSuccess)
end
it "returns the response body as a hash" do
- @response.params.should == Hash.from_xml(SUCCESS_BODY)['response']['item']
+ @response.params.should == Hash.from_xml(Mollie::SMS::TestHelper::SUCCESS_BODY)['response']['item']
end
it "returns whether or not it was a success" do
@@ -183,10 +148,7 @@
describe "A Mollie::SMS::Response instance, for a request that failed at the gateway" do
before do
- @http_response = Net::HTTPOK.new('1.1', '200', 'OK')
- @http_response.stubs(:read_body).returns(FAILURE_BODY)
- @http_response.add_field('Content-type', 'application/xml')
- @response = Mollie::SMS::Response.new(@http_response)
+ @response = Mollie::SMS.gateway_failure!
end
it "returns that the request was not a success" do
@@ -208,8 +170,7 @@
describe "A Mollie::SMS::Response instance, for a failed HTTP request" do
before do
- @http_response = Net::HTTPBadRequest.new('1.1', '400', 'Bad request')
- @response = Mollie::SMS::Response.new(@http_response)
+ @response = Mollie::SMS.http_failure!
end
it "returns an empty hash as the params" do
View
@@ -2,57 +2,11 @@
require "bacon"
require "mocha"
-$:.unshift File.expand_path("../../lib", __FILE__)
-
Bacon.summary_on_exit
-module Net
- class HTTP
- class << self
- attr_accessor :posted, :stubbed_response
-
- def reset!
- @posted = nil
- @stubbed_response = nil
- end
- end
-
- def host
- @address
- end
-
- def start
- yield self
- end
-
- def request(request)
- self.class.posted = [self, request]
- self.class.stubbed_response
- end
- end
-end
-
-class ResponseStub
-end
-
-SUCCESS_BODY = %{
-<?xml version="1.0" ?>
-<response>
- <item type="sms">
- <recipients>1</recipients>
- <success>true</success>
- <resultcode>10</resultcode>
- <resultmessage>Message successfully sent.</resultmessage>
- </item>
-</response>}
+$:.unshift File.expand_path("../../lib", __FILE__)
+require "mollie/sms"
-FAILURE_BODY = %{
-<?xml version="1.0"?>
-<response>
- <item type="sms">
- <recipients>1</recipients>
- <success>false</success>
- <resultcode>20</resultcode>
- <resultmessage>No username given.</resultmessage>
- </item>
-</response>}
+Mollie::SMS.username = 'AstroRadio'
+Mollie::SMS.password = 'secret'
+Mollie::SMS.originator = 'Astro INC'
Oops, something went wrong.

0 comments on commit af5355b

Please sign in to comment.