0
votes

I'm working with python mockito in my unit test. I'm familiar with the abilities of mockito, such as verify, mock, capture, etc., but I wonder how to verify the value of the method's fileds.

My production code.

class Dog(BaseModel):
    type: str
    age: int

    def bark(self, times: int) -> None:
        print(f"{self.type} {self.age}  {'ruf' * times}")


class FlowManager:

    def __init__(self, barks: int, dog_type: str, age: int):
        self.barks = barks
        self.dog_type = dog_type
        self.age = age

    def foo(self):
        # Some code....
        dog = Dog(type=self.dog_type, age=self.age)
        dog.bark(self.barks)
        # More some code...

And this is the unit test that covers the "foo" method of the "FlowManager" class.

from mockito import verify, when
class TestFlowManager(unittest.TestCase):

    def test_foo_happy_flow(self):
        # Arrange
        when(Dog).bark(...).thenReturn(None)

        # Act
        num_of_barks = 5
        dog_type = "bulldog"
        dog_age = 3
        FlowManager(num_of_barks, dog_type, dog_age).foo()

        # Assert
        verify(Dog).bark(num_of_barks)

My question is: How could I assert the properties of the Dog object. In other words: how could I assert the Dog class created with dog_type=="bulldog" and dog_age==3?

Thanks!

1
clarification: BaseModel class comes from Pydantic (from pydantic import BaseModel)HolyM

1 Answers

0
votes

It looks like you don't inject Dog into FlowManager or its methods. Within foo you call a modules global Dog so you probably have to intercept that:

when(module_under_test).Dog(type="bulldog", age=3).thenReturn(mock())

Since the when configuration is really specific you maybe don't need to verify anything. (foo has the type None -> None; it's just a side-effect out of the blue :grin:)