Skip to content
Browse files

Tests

  • Loading branch information...
1 parent e44f709 commit 262dbebcadc9ab1643402b036afa0958065283e0 Elad Meidar committed Feb 18, 2010
View
8 Rakefile
@@ -5,12 +5,14 @@ begin
require 'jeweler'
Jeweler::Tasks.new do |gem|
gem.name = "sinatra_fake_webservice"
- gem.summary = %Q{TODO: one-line summary of your gem}
- gem.description = %Q{TODO: longer description of your gem}
- gem.email = "elad@eizesus.com"
+ gem.summary = %Q{TODO: use a sinatra application in your Rails test environment to fake a remote web service that needs more magic than Fakeweb}
+ gem.description = %Q{TODO: FakeWeb allows you to fake a response from a specific url, this gem intends to give developers the option to allow several responses from the same url based on parameters (ex: WSDL)}
+ gem.email = "elad@nautilus6.com"
gem.homepage = "http://github.com/eladmeidar/sinatra_fake_webservice"
gem.authors = ["Elad Meidar"]
gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
+ gem.add_dependency 'sinatra'
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
end
Jeweler::GemcutterTasks.new
View
0 lib/sinatra_fake_webservice.rb
No changes.
View
36 lib/sinatra_webservice.rb
@@ -0,0 +1,36 @@
+
+
+class SinatraWebService
+ attr_reader :services
+
+ def initialize
+
+ end
+
+ def running?
+ false
+ end
+
+ def initialize
+ @services = []
+ end
+
+ def services
+ @services.dup.freeze
+ end
+
+ def run!
+ @services.each do |service|
+ puts "== Loading service #{service.name}"
+ service.run
+ end
+ end
+
+ def add_service(service = nil)
+ raise "Tried to add an empty service" if service.nil?
+ raise "Only WebService Objects are allowed" if !(service.is_a?(WebService))
+
+ @services << service
+ end
+
+end
View
33 lib/web_service.rb
@@ -0,0 +1,33 @@
+class WebService
+
+ class App < Sinatra::Base
+ get '/' do
+ "Hello! i am lindesy lohan!"
+ end
+ end
+
+ attr_accessor :port, :host, :name
+
+
+ # Initialize a new face service
+ def initialize(options = {})
+ @name = options[:name] ||= "App"
+ @port = options[:port] ||= "4567"
+ @host = options[:host] ||= "localhost"
+ end
+
+ def run
+ app = Thread.new("running_#{self.name}") do
+ App.run! :host => @host, :port => @port
+ end
+ sleep 1
+ end
+
+ def method_missing(method, *args, &block)
+ if App.respond_to?(method)
+ App.send(method, args, block)
+ else
+ super
+ end
+ end
+end
View
9 metal/sinatra_stem.rb
@@ -0,0 +1,9 @@
+require 'sinatra/base'
+
+class SinatraStem < Sinatra::Base
+
+ get '/' do
+ "Hellllllo! i am Lindsey Lohan!!"
+ end
+end
+
View
7 test/helper.rb
@@ -1,10 +1,15 @@
require 'rubygems'
require 'test/unit'
require 'shoulda'
+require 'net/http'
+require 'uri'
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
+$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'metal'))
$LOAD_PATH.unshift(File.dirname(__FILE__))
-require 'sinatra_fake_webservice'
+require 'sinatra_stem'
+require 'sinatra_webservice'
+require 'web_service'
class Test::Unit::TestCase
end
View
43 test/test_sinatra_fake_webservice.rb
@@ -1,7 +1,46 @@
require 'helper'
class TestSinatraFakeWebservice < Test::Unit::TestCase
- should "probably rename this file and start testing for real" do
- flunk "hey buddy, you should probably rename this file and start testing for real"
+
+ context "a new sinatra web service" do
+
+ setup do
+ @sinatra_web_service = SinatraWebService.new
+ end
+
+ should "fails adding a new service explicitly" do
+ assert_raises(TypeError) do
+ @sinatra_web_service.services << WebService.new
+ end
+ assert @sinatra_web_service.services.empty?
+ end
+
+ should "fails assiging a new services array" do
+ assert_raises(NoMethodError) do
+ @sinatra_web_service.services = [WebService.new]
+ end
+ assert @sinatra_web_service.services.empty?
+ end
+
+ should "not be running by default" do
+ @sinatra_web_service.add_service(WebService.new)
+ assert !(@sinatra_web_service.running?)
+ end
+
+ should "run when !run is invoked" do
+ @sinatra_web_service.add_service(WebService.new)
+ @sinatra_web_service.run!
+ end
+
+ should "allow adding a new service with #add_service" do
+ @sinatra_web_service.add_service(WebService.new)
+ assert @sinatra_web_service.services.any?
+ end
+
+ should "allow running all the sinatra subapplications" do
+ @sinatra_web_service.add_service(WebService.new)
+ end
end
+
+
end
View
39 test/test_web_service.rb
@@ -0,0 +1,39 @@
+require 'helper'
+
+class TestWebservice < Test::Unit::TestCase
+
+ context "a new web service" do
+ setup do
+ @web_service = WebService.new
+ end
+
+ should "have a default name" do
+ assert_equal "App", @web_service.name
+ end
+
+ should "have be set to localhost on port 4567" do
+ assert_equal "localhost", @web_service.host
+ assert_equal "4567", @web_service.port
+ end
+
+ context "with a custom method" do
+ setup do
+ @web_service = WebService.new
+ @webservice.get "/test" do
+ "Hiya!"
+ end
+
+ @webservice.run
+ end
+
+ should "respond to '/test' with 'Hiya!'" do
+ res = Net::HTTP.start(@webservice.host,@webservice.port) do |http|
+ http.get('/test')
+ end
+ puts res.body
+ assert_equal "Hiya!", res.body
+ end
+ end
+ end
+
+end

0 comments on commit 262dbeb

Please sign in to comment.
Something went wrong with that request. Please try again.