我如何使用 unittest 模拟嵌套构造函数中的属性

How do i mock an attribute within nested constructor using unittest

这就是我要测试的内容:

src/DBOps.py

class DBOps() :
    class dynamodbOps() :
        def __init__(self,db_table) :
            dynamodb = boto3.resource('dynamodb')
            self.table = dynamodb.Table(db_table)
        def GetDBItem(self,pool):
            response = self.table.get_item(Key={'pool' : pool})

这是不成功的测试用例:

class test_GetDBItem(unittest.TestCase) :
    def setUp(self):
        self.db_ops = DBOps()
        db_table = "test_table"
        self.dyn = self.db_ops.dynamodbOps(db_table)

    @mock.patch('src.DBOps')
    def test_run_query_with_item(self, getItem_mock) :
        getItem_mock.dynamodbOps.table.get_item.return_value = {"Blah":"Blah Blah"}
        pool = 'some_pool'
        response = self.dyn.GetDBItem(pool)

我正在尝试模拟 'table.get_item' 调用,但无论如何都失败了。任何指针都会很棒!

计算如下:

class test_GetDBItem(unittest.TestCase) :
  def setUp(self):
      db_ops = DBOps()
      db_table = "test_table"
      self.dyn = db_ops.dynamodbOps(db_table)

  def test_run_query_with_item(self) :
      with patch.object(self.dyn.table, 'get_item') as getItem_mock :
        getItem_mock.return_value = {"Blah":"Blah Blah"}
        pool = 'some pool'
        response = self.dyn.GetDBItem(quadra_pool)