Created
May 19, 2011 20:33
-
-
Save bhenderson/981674 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "minitest/autorun" | |
require "post_stats" | |
class PostStats | |
class << self | |
attr_accessor :client | |
end | |
end | |
class Net::HTTP | |
class << self | |
alias old_start start | |
end | |
end | |
class TestPostStats < MiniTest::Unit::TestCase | |
def setup | |
PostStats.app_id = "my_app" | |
PostStats.app_env = :development | |
@client = PostStats.new | |
PostStats.client = @client | |
def (Net::HTTP).start(*a) | |
Net::HTTPOK.new 200,2,3 | |
end | |
super | |
end | |
def test_class_seters | |
PostStats.app_id = "foo" | |
PostStats.app_env = :development | |
assert_equal "foo", PostStats.client.app_id | |
assert_equal :development, PostStats.client.app_env | |
end | |
def test_class_app_env_raises | |
assert_raises ArgumentError do | |
PostStats.app_env = "foo" | |
end | |
end | |
def test_class_post_metric | |
time = Time.now | |
metric = :metric | |
value = 1 | |
notes = "hi" | |
@mock_client = MiniTest::Mock.new | |
@mock_client.expect :post_metric, true, [time, metric, value, notes] | |
PostStats.client = @mock_client | |
PostStats.post_metric time, metric, value, notes | |
@mock_client.verify | |
pass | |
end | |
def test_class_notice_deployment | |
time = Time.now | |
notes = "hi" | |
@mock_client = MiniTest::Mock.new | |
@mock_client.expect :post_metric, true, [time, :deployment, 1, notes] | |
PostStats.client = @mock_client | |
PostStats.notice_deployment time, notes | |
@mock_client.verify | |
pass | |
end | |
def test_post_metric | |
time = Time.utc 2011 | |
metric = :metric | |
value = 1 | |
notes = "hi" | |
def @client.post path, notes | |
@path = path | |
end | |
@client.post_metric time, metric, value, notes | |
path = "/stats/2011-01-01T00:00:00+00:00/my_app/metric/1" | |
assert_equal path, @client.instance_variable_get(:@path) | |
end | |
def test_app_uri | |
assert_kind_of URI, @client.app_uri | |
end | |
def test_convert_time_raises | |
assert_raises ArgumentError do | |
@client.convert_time "not a time" | |
end | |
end | |
def test_convert_time | |
assert_equal "2011-01-01T08:00:00+00:00", @client.convert_time(Time.local(2011)) | |
end | |
def test_valid_response | |
@client.post "/" | |
pass | |
end | |
def test_invalid_response | |
def (Net::HTTP).start(*a) end | |
assert_raises PostStats::ResponseError do | |
@client.post "/" | |
end | |
end | |
def test_invalid_connection | |
def (Net::HTTP).start(*a) | |
raise Errno::ECONNREFUSED | |
end | |
assert_raises PostStats::ConnectionError do | |
@client.post "/" | |
end | |
end | |
class MockHTTP | |
attr_accessor :notes | |
def request(req, notes) @notes = notes end | |
end | |
def test_post_data_receives_flat_string | |
def (Net::HTTP).start(*a) | |
@mock_http = MockHTTP.new | |
yield @mock_http | |
msg = "expected form data to be a flat string. got `#{@mock_http.notes}'" | |
raise MockExpectationError, msg unless @mock_http.notes == "my data" | |
Net::HTTPOK.new 200,2,3 | |
end | |
@client.post "/", "my data" | |
pass | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment