My code is organized as several independent classes. Each dependency is injected by a setter, so in the end I create instances of classes B
and C
, set their parameters and inject them to class A
.
Now I want to test class A
in isolation, so my first intuition is to create mocks for B
and C
. My problem is that methods from B
and C
are called multiple times with different parameters and return different results. How can I mock that?
Pseudo-code:
b = B()
b.set_param1(value1)
b.set_param2(value2)
c = C()
c.set_param1(value1)
c.set_param2(value2)
a = A()
a.set_b(b)
a.set_c(c)
a.run():
X = self.get_b().method1(x) # should return 1
Y = self.get_b().method2(y) #s hould return 2
Z = self.get_c().method1(z) # should return a complicated dataframe
W = self.get_c().method2(w)# should return yet another complicated dataframe
# process W X Y Z ...
Everything I read on mocking in python is limited to very simple examples where mocking can happen locally.
I know I can set a sequence of return values, but this is limited to a single method and is not future proof if the code logic changes.
Is it even possible or correct to test such a hierarchy?
is not future proof if the code logic changes
, there is no such thing a future proof in my personal opinion. We will always need to go back to modify our old codes in some way to accommodate future changes.