Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add Test::Unit test to verify test_helper works.

This also fixes up various issues in test_helper that stoped Test::Unit from
working. Remove mock in favour of a simpler, agnostic solution. Renamed
test_request to create_test_request so it wouldn't get picked up as an
actual test.

Closes #133.
  • Loading branch information...
commit 1a1b25b22b8eb8eeb216d867787965e84427099a 1 parent 9e4c10f
@dj2 dj2 authored
View
12 Rakefile
@@ -3,9 +3,17 @@ Bundler::GemHelper.install_tasks
require 'yard'
require 'rspec/core/rake_task'
+require 'rake/testtask'
-task :default => [:spec]
-task :test => [:spec]
+task :default => [:test]
+task :test => [:spec, :unit]
+
+desc "run the unit test"
+Rake::TestTask.new(:unit) do |t|
+ t.libs = "test"
+ t.test_files = FileList['test/**/*_test.rb']
+ t.verbose = true
+end
desc "run spec tests"
RSpec::Core::RakeTask.new('spec') do |t|
View
32 lib/goliath/test_helper.rb
@@ -1,6 +1,7 @@
require 'em-synchrony'
require 'em-synchrony/em-http'
+require 'goliath/api'
require 'goliath/server'
require 'goliath/rack'
require 'rack'
@@ -50,9 +51,8 @@ def server(api, port, options = {}, &blk)
end
def setup_logger(opts)
- if opts[:log_file].nil? && opts[:log_stdout].nil?
- return mock('log').as_null_object
- end
+ return fake_logger if opts[:log_file].nil? && opts[:log_stdout].nil?
+
log = Log4r::Logger.new('goliath')
log_format = Log4r::PatternFormatter.new(:pattern => "[#{Process.pid}:%l] %d :: %m")
log.level = opts[:verbose].nil? ? Log4r::INFO : Log4r::DEBUG
@@ -74,7 +74,7 @@ def setup_logger(opts)
#
# @return [Nil]
def stop
- EM.stop
+ EM.stop_event_loop
end
# Wrapper for launching API and executing given code block. This
@@ -111,7 +111,7 @@ def hookup_request_callbacks(req, errback, &blk)
# @param errback [Proc] An error handler to attach
# @param blk [Proc] The callback block to execute
def head_request(request_data = {}, errback = nil, &blk)
- req = test_request(request_data).head(request_data)
+ req = create_test_request(request_data).head(request_data)
hookup_request_callbacks(req, errback, &blk)
end
@@ -121,7 +121,7 @@ def head_request(request_data = {}, errback = nil, &blk)
# @param errback [Proc] An error handler to attach
# @param blk [Proc] The callback block to execute
def get_request(request_data = {}, errback = nil, &blk)
- req = test_request(request_data).get(request_data)
+ req = create_test_request(request_data).get(request_data)
hookup_request_callbacks(req, errback, &blk)
end
@@ -131,7 +131,7 @@ def get_request(request_data = {}, errback = nil, &blk)
# @param errback [Proc] An error handler to attach
# @param blk [Proc] The callback block to execute
def post_request(request_data = {}, errback = nil, &blk)
- req = test_request(request_data).post(request_data)
+ req = create_test_request(request_data).post(request_data)
hookup_request_callbacks(req, errback, &blk)
end
@@ -141,7 +141,7 @@ def post_request(request_data = {}, errback = nil, &blk)
# @param errback [Proc] An error handler to attach
# @param blk [Proc] The callback block to execute
def put_request(request_data = {}, errback = nil, &blk)
- req = test_request(request_data).put(request_data)
+ req = create_test_request(request_data).put(request_data)
hookup_request_callbacks(req, errback, &blk)
end
@@ -151,13 +151,23 @@ def put_request(request_data = {}, errback = nil, &blk)
# @param errback [Proc] An error handler to attach
# @param blk [Proc] The callback block to execute
def delete_request(request_data = {}, errback = nil, &blk)
- req = test_request(request_data).delete(request_data)
+ req = create_test_request(request_data).delete(request_data)
hookup_request_callbacks(req, errback, &blk)
end
- def test_request(request_data)
+ def create_test_request(request_data)
path = request_data.delete(:path) || ''
EM::HttpRequest.new("http://localhost:#{@test_server_port}#{path}")
end
+
+ private
+
+ def fake_logger
+ Class.new do
+ def method_missing(name, *args, &blk)
+ nil
+ end
+ end.new
+ end
end
-end
+end
View
25 test/echo_test.rb
@@ -0,0 +1,25 @@
+require 'test_helper'
+
+class Echo < Goliath::API
+ use Goliath::Rack::Params
+
+ def response(env)
+ [200, {}, env.params['echo']]
+ end
+end
+
+class EchoTest < Test::Unit::TestCase
+ include Goliath::TestHelper
+
+ def setup
+ @err = Proc.new { assert false, "API request failed" }
+ end
+
+ def test_query_is_echoed_back
+ with_api(Echo) do
+ get_request({:query => {:echo => 'test'}}, @err) do |c|
+ assert_equal 'test', c.response
+ end
+ end
+ end
+end
View
5 test/test_helper.rb
@@ -0,0 +1,5 @@
+
+require 'test/unit'
+require 'goliath/test_helper'
+
+Goliath.env = :test

0 comments on commit 1a1b25b

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