类中的python模拟依赖项

2024-09-19 23:32:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我有类似于下面的内容,我想测试MailReader find方法为传输设置mock,但是当我尝试只模拟消息时,传输尝试打开连接时会出错。在

class Transport(object):
       def __init__(self, hostname, username, password, ssl):
          # something 
          pass
      def connect(self):
          pass
      def messages(self):
          pass


class MailReader(object):
  def __init__(self, hostname, username, password, ssl):
        super(Mail, self).__init__()
        self.hostname = hostname
        self.username = username
        self.password = password
        self.ssl = ssl
        self.connect()
  def connect(self):
        """
         connect to the email box
        """
        self.connection = Transport(self.hostname, self.username, self.password,
                                self.ssl)
   def find(self, search):
        messages_folder = self.connection.messages()

Tags: selfsslobjectinitdefconnectusernamepass