Created
April 22, 2010 22:04
-
-
Save gma/375882 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
# I'm rather pleased with this use of Python decorators for applying | |
# stubs to a class before a test method. | |
# | |
# It feels a bit like a poor man's shoulda. :-) | |
import os | |
import unittest2 as unittest | |
import satisfaction | |
def fixture(cls, name): | |
def wrapper(test): | |
def test_with_fixture(*args): | |
def stubbed_url(self, topic_id): | |
fixture = "%s-%s.xml" % (cls.__name__.lower(), name) | |
return os.path.join(os.getcwd(), "fixtures", fixture) | |
try: | |
original_url = cls.url | |
cls.url = stubbed_url | |
return test(*args) | |
finally: | |
cls.url = original_url | |
return test_with_fixture | |
return wrapper | |
class TopicTest(unittest.TestCase): | |
def topic(self, topic_id="409678"): | |
return satisfaction.Topic(topic_id) | |
def test_when_topic_doesnt_exist_then_not_found(self): | |
with self.assertRaises(satisfaction.ResourceNotFound): | |
self.topic("bad-id").title | |
def test_when_topic_exists_then_title_available(self): | |
self.assertEqual(self.topic().title, "Fantastic improvement") | |
def test_when_topic_exists_then_content_available(self): | |
self.assertIn("Well done!", self.topic().content) | |
@fixture(satisfaction.Topic, "without-replies") | |
def test_when_topic_has_no_replies_then_reply_count_zero(self): | |
self.assertEqual(0, self.topic().reply_count) | |
@fixture(satisfaction.Topic, "with-replies") | |
def test_when_topic_has_replies_then_reply_count_set(self): | |
self.assertEqual(3, self.topic().reply_count) | |
if __name__ == "__main__": | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment