【问题标题】:Mocking a Peewe class does not return value模拟 Peewe 类不会返回值
【发布时间】:2020-01-13 19:07:30
【问题描述】:

模拟一个peewe类来模拟类属性

models.py

import peewe

class A():
    human = CharField()


class B():
    first = CharField()
    second = ForeignKeyField(B, related_name="addresses")

test.py 从模拟导入补丁

def build_A_model():
   var = models.A()
   var.human = "yes"

def build_B_model(A_fake_model):
   var1 = models.B()
   var1.first = "test"
   var1.second = A_fake_model
   return var1

@patch(models.A)
 class testing(unittest.TestCase):
     def test1(self, A_mock):
         A_fake_model = build_A_model()
         B_model = build_B_model(A_fake_model)
         expected = {
            "first": "test",
            "second": {
                "human": "yes"
            },
          }
         # some function to searlize B_model to json
         result = json_searlized_data
         self.assertEqual(expected, result)

任务:试图模拟 A_Model 和 B_model 以获得期望的预期结果:

问题: Mock_A 类返回一个 Magic Mock() 而不是返回值

错误: 人:MagicMock name='xxxxx' id='139670936802896'"

问题:如何返回mocked class_A模型的值?

【问题讨论】:

    标签: python unit-testing mocking mockito peewee


    【解决方案1】:

    你没有返回任何东西:

    def build_A_model():
       var = models.A()
       var.human = "yes"
    

    添加return语句哟。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-09-09
      • 1970-01-01
      • 2022-11-07
      • 2019-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多