Ложный метод для возврата разных значений - PullRequest
0 голосов
/ 28 января 2020

как смоделировать метод, который был вызван 2 раза, и заставить его возвращать разные значения. Для наглядности вот код:

def method(self, param):
   # here I have the logic if param is equal to 1 -> return [1,2,3], if param is equal to 2 -> return [a,s,d]

def method_x(self, param_x):
   # here uses method returned values with 1 or 2

def main():
    one = self.method(1)
    two = self.method(2)

    three = self.method_x(one)
    four = self.method_x(two)

test:

def setUp(self):
    self.c = py_file.Class()

def test_main(self):
    self.c.method = MagicMock()
    self.c.main()

    self.c.method.assert_any_call(1)
    self.c.method.assert_any_call(2)
    self.assertEqual(2, self.c.method.call_count)

    # but the problem comes when I need to use for other methods method(1) in three and method(2) in four
    ...

Я пытался использовать side_effect вот так:

self.c.method = MagicMock(side_effect=[[1,2,3],[a,s,d]])
# this way running test one has 1,2,3 values and two has a,s,d

, но потом когда дело доходит до использования значений для four и five, которые я получаю (например, c.method.side_effects= [[1,2,3],[a,s,d]] and then for four use c.method.side_effects[0] and for five with [1]:

TypeError: 'listiterator' object is unsubscriptable

Как передать правильные значения для исправления переменных в тесте - как смоделировать и установить возврат правильные значения?

Использование Python2 .6.6, макет 1.0.0

...