2021年4月6日星期二

Mock.patch gives AttributeError

I'm trying to get my head around why I can't access the method when mocking.

Here's the class with the method that I want to test / mock:

import sqlite3  from customer import Customer    class DataChecker:      def __init__(self):          self.conn = sqlite3.connect('pos.db')          self.cursor = self.conn.cursor()        def find_customer(self, customerID):          query = "SELECT * FROM Customers WHERE ID == ?;"          customers = self.execute_db(query, customerID, True)                    if len(customers) == 0:              print("Customer with given ID not found in DB")              return False          else:              print("Customer found")              return True  

Mock:

from datachecker import DataChecker  from unittest import mock    @mock.patch('datachecker.find_customer')  def test_find_customer_true(mocker):      datachecker.execute_db.return_value = {'customers': 10}      assert datachecker.find_customer() == True  
https://stackoverflow.com/questions/66978311/mock-patch-gives-attributeerror April 07, 2021 at 09:07AM

没有评论:

发表评论