Spyfi
项目描述
Spyfi
将现有类快速转换为间谍的一种方法。
为什么要这样做呢?
我经常通过围绕列表包装接口来为我的测试创建间谍,例如。
class FakeEmailSender(list):
def send(self, address: str, message: str) -> None:
self.append((address, message))
def test_when_a_customer_signs_up():
sender = FakeEmailSender()
handler = SignupHandler(sender)
handler("user@domain.com", "password")
assert (("user@domain.com", "welcome to the website")) in sender
有时这会有些麻烦,尤其是当你需要监视对象层次结构时。Spyfi,发音为“spiffy”,是一种快速为Python对象图添加工具并捕获对其调用的一种方式。
安装
$ pip install spyfi
用法
from spyfi import Spy
class Thing:
def __init__(self, colour):
self.colour = colour
def say_hello(self, message):
print(f"Hello, I am a {self.colour} thing: {message})
class ThingFactory:
def make_thing(self, colour:str) -> Thing:
return Thing(colour)
def test_thing_messages():
# Spiffy takes any old object and wraps its methods
# so that an arbitrary callback receives args and kwargs.
# In this case, we're appending all calls to a list.
spy = Spy(ThingFactory())
# The returned object is otherwise unchanged. `factory` is a real
# ThingFactory and behaves as normal.
factory = spy.target
factory.make_thing("blue").say_hello("I like python")
# Since we have access to the calls list, we can assert that
# particular methods were called with the right data.
assert len(spy.calls) == 2
assert calls[0].method == "make_thing"
assert calls[0].args == ("blue",)
# Spyfi includes a helper method to make assertions easier
assert spy.has("say_hello")
assert spy.has("say_hello", "I like python")
贡献
非常欢迎贡献。要了解更多信息,请参阅贡献指南。
许可证
在MIT许可证下分发,Spyfi是免费和开源软件。
问题
如果您遇到任何问题,请提交问题并附上详细描述。
致谢
该项目是从@cjolowicz的Hypermodern Python Cookiecutter模板生成的。
项目详情
下载文件
下载适用于您平台文件。如果您不确定选择哪一个,请了解更多关于 安装包 的信息。
源分布
spyfi-1.0.0a0.tar.gz (5.3 kB 查看哈希值)
构建分布
spyfi-1.0.0a0-py3-none-any.whl (4.6 kB 查看哈希值)