Skip to content

Instantly share code, notes, and snippets.

@oyakata
Created July 24, 2011 07:08
Show Gist options
  • Save oyakata/1102364 to your computer and use it in GitHub Desktop.
Save oyakata/1102364 to your computer and use it in GitHub Desktop.
シャチは決して人を襲わないことをminimockを使ってテスト
# -*- coding: utf-8 -*-
class Animal(object):
def ouch(self):
return u"あ痛"
class Human(Animal):
pass
class Shark(Animal):
pass
# -*- coding: utf-8 -*-
from .animals import Human
class Orca(object):
def hurt(self, animal):
if not isinstance(animal, Human):
animal.ouch()
# -*- coding: utf-8 -*-
import unittest
from minimock import mock, Mock, TraceTracker, restore
import animals
import orca
class OrcaTest(unittest.TestCase):
"""シャチは決して人を襲いません。そのことを証明します。 """
def setUp(self):
tracker = TraceTracker()
mock("animals.Animal.ouch", mock_obj=Mock("ouch", tracker=tracker))
self.tracker = tracker
def tearDown(self):
restore()
def test_never_hurt_human(self):
"""Humanは決して攻撃しないことを検証する。 """
killer_whale = orca.Orca()
killer_whale.hurt(animals.Human())
self.assertEqual(self.tracker.check("Called ouch()"), False)
def test_hurt_someone_else_but_human(self):
"""Humanでなければ攻撃することを検証する。 """
killer_whale = orca.Orca()
killer_whale.hurt(animals.Shark())
self.assertEqual(self.tracker.check("Called ouch()"), True)
if __name__ == "__main__":
unittest.main()
# -*- coding:utf-8 -*-
def foo():
return 9
def test():
from minimock import mock, restore, TraceTracker
tracker = TraceTracker()
mock("foo", tracker=tracker)
try:
foo()
assert tracker.check("Called foo()") == True
finally:
restore()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment