0
votes

Mock a peewe class to mock the class attributes

models.py

import peewe

class A():
    human = CharField()


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

test.py from mock import patch

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)

Task: Trying to mock A_Model and B_model to get the desired expected result:

Issue: Mock_A class returns a Magic Mock() instead of returning a value

Error: human: MagicMock name='xxxxx' id='139670936802896'"

Question: How to return a value of mocked class_A model?

1

1 Answers

0
votes

You are not returning anything:

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

Add a return statement yo.