|
1 | 1 | #!/usr/bin/env python |
2 | 2 | # -*- coding: utf-8 -*- |
3 | 3 | import unittest |
4 | | -from creational.abstract_factory import PetShop,\ |
5 | | - Dog, Cat, DogFactory, CatFactory, Pet |
| 4 | +from creational.abstract_factory import PetShop, Dog |
6 | 5 | try: |
7 | 6 | from unittest.mock import patch |
8 | 7 | except ImportError: |
|
12 | 11 | class TestPetShop(unittest.TestCase): |
13 | 12 |
|
14 | 13 | def test_dog_pet_shop_shall_show_dog_instance(self): |
15 | | - f = DogFactory() |
16 | | - with patch.object(f, 'get_pet') as mock_f_get_pet,\ |
17 | | - patch.object(f, 'get_food') as mock_f_get_food: |
18 | | - ps = PetShop(f) |
19 | | - ps.show_pet() |
20 | | - self.assertEqual(mock_f_get_pet.call_count, 1) |
21 | | - self.assertEqual(mock_f_get_food.call_count, 1) |
22 | | - |
23 | | - def test_cat_pet_shop_shall_show_cat_instance(self): |
24 | | - f = CatFactory() |
25 | | - with patch.object(f, 'get_pet') as mock_f_get_pet,\ |
26 | | - patch.object(f, 'get_food') as mock_f_get_food: |
27 | | - ps = PetShop(f) |
28 | | - ps.show_pet() |
29 | | - self.assertEqual(mock_f_get_pet.call_count, 1) |
30 | | - self.assertEqual(mock_f_get_food.call_count, 1) |
31 | | - |
32 | | - |
33 | | -class TestCat(unittest.TestCase): |
34 | | - |
35 | | - @classmethod |
36 | | - def setUpClass(cls): |
37 | | - cls.c = Cat() |
38 | | - |
39 | | - def test_cat_shall_meow(cls): |
40 | | - cls.assertEqual(cls.c.speak(), 'meow') |
41 | | - |
42 | | - def test_cat_shall_be_printable(cls): |
43 | | - cls.assertEqual(str(cls.c), 'Cat') |
44 | | - |
45 | | - |
46 | | -class TestDog(unittest.TestCase): |
47 | | - |
48 | | - @classmethod |
49 | | - def setUpClass(cls): |
50 | | - cls.d = Dog() |
51 | | - |
52 | | - def test_dog_shall_woof(cls): |
53 | | - cls.assertEqual(cls.d.speak(), 'woof') |
54 | | - |
55 | | - def test_dog_shall_be_printable(cls): |
56 | | - cls.assertEqual(str(cls.d), 'Dog') |
57 | | - |
58 | | - |
59 | | -class PetTest(unittest.TestCase): |
60 | | - |
61 | | - def test_from_name(self): |
62 | | - test_cases = [("kitty", "Miao"), ("duck", "Quak")] |
63 | | - for name, expected_speech in test_cases: |
64 | | - pet = Pet.from_name(name) |
65 | | - self.assertEqual(pet.speak(), expected_speech) |
| 14 | + dog_pet_shop = PetShop(Dog) |
| 15 | + with patch.object(Dog, 'speak') as mock_Dog_speak: |
| 16 | + dog_pet_shop.show_pet() |
| 17 | + self.assertEqual(mock_Dog_speak.call_count, 1) |
0 commit comments