0

Mock a DB query:I am trying to mock a db query and assert that save is called but it is failing with assertion. can someone please help?

models.py

import peewee

class A():
    human = CharField()
    Age  = CharField()

app.py

def get_A():
  return models.select().where(models.A.Age="5")

def update_A():
  age = get_A()
  for i in age:
     i.Age = 10
     i.save()

test.py

def build_mock_A():
  a = A()
  a.human = "hath"
  a.Age.  = 5
  return a

@patch('app.get_A')
def test_A(self,mock_get_A):
   mock_get_A.return_value = build_mock_A()
   app.update_A()
   mock_get_A.save.assert_called_once()

Error:

 raise AssertionError(msg)
 AssertionError: Expected 'save' to have been called once. Called 0 times.
immrsteel
  • 1,333
  • 4
  • 13
  • 18
  • Calling `mock_get_A` returns the result of `build_mock_A`, which appears to be a real `A`. It's unclear why you think that would correspond with `mock_get_A.save`. – jonrsharpe Apr 08 '20 at 19:43
  • @jonrsharpe build_mock_A was to return the value of get_A(). Even if i change build_mock_A() to a=Mock() instead of a=A().It gives me the same error. – immrsteel Apr 08 '20 at 20:24
  • Look at how `get_A` and its return value are *used*. Your mock needs to implement the same interface. – jonrsharpe Apr 08 '20 at 20:26

0 Answers0