Permalink
Browse files

fixed tests to run offline by stubbing call to get forum key

  • Loading branch information...
1 parent d50c65d commit ba50275c659fad3027a7fa757230e93f82d1fe96 Matthew Van Horn committed Dec 2, 2008
Showing with 26 additions and 5 deletions.
  1. +18 −3 lib/disqus/forum.rb
  2. +3 −2 test/forum_test.rb
  3. +4 −0 test/test_helper.rb
  4. +1 −0 test/thread_test.rb
View
@@ -19,7 +19,9 @@ def ==(other_forum)
def self.list(opts = {})
response = Disqus::Api::get_forum_list(opts)
if response["succeeded"]
- list = response["message"].map{|forum| Forum.new(forum["id"], forum["shortname"], forum["name"], forum["created_at"])}
+ return response["message"].map{|forum| Forum.new(forum["id"], forum["shortname"], forum["name"], forum["created_at"])}
+ else
+ raise_api_error(response)
end
end
@@ -32,7 +34,11 @@ def self.find(forum_id, opts = {})
def load_key(opts = {})
response = Disqus::Api::get_forum_api_key(opts.merge(:forum_id => self.id))
- @key = response["message"] if response["succeeded"]
+ if response["succeeded"]
+ return @key = response["message"]
+ else
+ raise_api_error(response)
+ end
end
def key
@@ -48,15 +54,19 @@ def get_thread_by_url(url, opts = {})
if response["succeeded"]
t = response["message"]
Thread.new(t["id"], self, t["slug"], t["title"], t["created_at"], t["allow_comments"], t["url"], t["identifier"])
+ else
+ raise_api_error(response)
end
end
- def get_thread_by_identifier(identifier, title, opts = {})
+ def thread_by_identifier(identifier, title, opts = {})
# TODO - should we separate thread retrieval from thread creation? The API to me seems confusing here.
response = Disqus::Api::thread_by_identifier(opts.merge(:identifier => identifier, :title => title, :forum_api_key => key))
if response["succeeded"]
t = response["message"]["thread"]
Thread.new(t["id"], self, t["slug"], t["title"], t["created_at"], t["allow_comments"], t["url"], t["identifier"])
+ else
+ raise_api_error(response)
end
end
@@ -70,5 +80,10 @@ def update_thread(id, title, slug, url, allow_comments, opts = {})
return result["succeeded"]
end
+ protected
+
+ def raise_api_error(response)
+ raise "Error: #{response['code']}: #{response['message']}"
+ end
end
end
View
@@ -5,6 +5,7 @@ class ForumTest < Test::Unit::TestCase
def setup
require 'disqus'
Disqus.defaults[:api_key] = DISQUS_TEST["api_key"]
+ stub_api_call(:get_forum_api_key)
end
def test_forum_list
@@ -41,10 +42,10 @@ def test_get_thread_by_url
assert_equal expected, thread
end
- def test_get_thread_by_identifier
+ def test_thread_by_identifier
mock_api_call(:thread_by_identifier)
forum = create_forum
- thread = forum.get_thread_by_identifier("FAKE_IDENTIFIER", "")
+ thread = forum.thread_by_identifier("FAKE_IDENTIFIER", "")
expected = Disqus::Thread.new("7651269", forum, "test_thread", "Test thread", "2008-11-28T01:47", true, "FAKE_URL", "FAKE_IDENTIFIER")
assert_equal expected, thread
end
View
@@ -24,3 +24,7 @@ def mock_api_call(method_name)
Disqus::Api.expects(method_name.to_sym).returns(JSON.parse(File.read(File.dirname(__FILE__) + "/responses/#{method_name}.json")))
end
+def stub_api_call(method_name)
+ Disqus::Api.stubs(method_name.to_sym).returns(JSON.parse(File.read(File.dirname(__FILE__) + "/responses/#{method_name}.json")))
+end
+
View
@@ -5,6 +5,7 @@ class ThreadTest < Test::Unit::TestCase
def setup
require 'disqus'
Disqus.defaults[:api_key] = DISQUS_TEST["api_key"]
+ stub_api_call(:get_forum_api_key)
end
def test_thread_list

0 comments on commit ba50275

Please sign in to comment.