I had the opportunity to make a mock with many attributes, so I investigated how to specify the attributes. The third method introduced to expand and pass the dict seems to be easy to use.
This is OK for simple cases
mock = Mock()
mock.hoge = "HOGE"
You can use this method to set attributes other than the keywords used in the argument. You can check the argument structure at here.
mock = Mock(hoge="HOGE")
I personally like this. I feel that it is easy to see especially when the number increases.
mock = Mock(**{
"hoge" : "HOGE"
})
mock = Mock()
mock.configure_mock(**{
"hoge" : "HOGE"
})
Recommended Posts