Имитировать атрибуты в макете Python?Python

Программы на Python
Anonymous
Имитировать атрибуты в макете Python?

Сообщение Anonymous »

Мне довольно сложно использовать макет в Python:

Код: Выделить всё

def method_under_test():
r = requests.post("http://localhost/post")

print r.ok # prints ""

if r.ok:
return StartResult()
else:
raise Exception()

class MethodUnderTestTest(TestCase):

def test_method_under_test(self):
with patch('requests.post') as patched_post:
patched_post.return_value.ok = True

result = method_under_test()

self.assertEqual(type(result), StartResult,
"Failed to return a StartResult.")
Тест на самом деле возвращает правильное значение, но r.ok — это объект Mock, а не True. Как имитировать атрибуты в библиотеке макета Python?

Подробнее здесь: https://stackoverflow.com/questions/168 ... ython-mock

Вернуться в «Python»