What is the difference between mock.patch.object(… and mock.patch(

前端 未结 1 1413
温柔的废话
温柔的废话 2021-01-30 03:47

I am trying to understand the difference between these two approaches of mocking a method. Could someone please help distinguish them? For this example, I use the passlib libr

相关标签:
1条回答
  • 2021-01-30 04:19

    You already discovered the difference; mock.patch() takes a string which will be resolved to an object when applying the patch, mock.patch.object() takes a direct reference.

    This means that mock.patch() doesn't require that you import the object before patching, while mock.patch.object() does require that you import before patching.

    The latter is then easier to use if you already have a reference to the object.

    0 讨论(0)
提交回复
热议问题