【编程思想】【设计模式】【测量模式Testability】Setter_injection
2024-09-05 05:56:33
Python版
https://github.com/faif/python-patterns/blob/master/dft/setter_injection.py
#!/usr/bin/python
# -*- coding : utf-8 -*-
import datetime """
Port of the Java example of "Setter Injection" in
"xUnit Test Patterns - Refactoring Test Code" by Gerard Meszaros
(ISBN-10: 0131495054, ISBN-13: 978-0131495050) accessible in outdated version on
http://xunitpatterns.com/Dependency%20Injection.html. production code which is untestable: class TimeDisplay(object): def __init__(self):
self.time_provider = datetime.datetime def get_current_time_as_html_fragment(self):
current_time = self.time_provider.now()
current_time_as_html_fragment = "<span class=\"tinyBoldText\">{}</span>".format(current_time)
return current_time_as_html_fragment
""" class TimeDisplay(object): def __init__(self):
pass def set_time_provider(self, time_provider):
self.time_provider = time_provider def get_current_time_as_html_fragment(self):
current_time = self.time_provider.now()
current_time_as_html_fragment = "<span class=\"tinyBoldText\">{}</span>".format(current_time)
return current_time_as_html_fragment class ProductionCodeTimeProvider(object):
"""
Production code version of the time provider (just a wrapper for formatting
datetime for this example).
""" def now(self):
current_time = datetime.datetime.now()
current_time_formatted = "{}:{}".format(current_time.hour,
current_time.minute)
return current_time_formatted class MidnightTimeProvider(object):
"""
Class implemented as hard-coded stub (in contrast to configurable stub).
""" def now(self):
current_time_is_always_midnight = "24:01"
return current_time_is_always_midnight
Python转载版
最新文章
- syntax error near unexpected token‘(
- 使用idea debug多线程
- Ajax基本概念和原理
- 2015 年开源前端框架盘点 TOP 20
- loadrunner 发送gzip压缩json格式(转)
- 对AppStore中的项目进行评分(转载)
- 离散型特征编码方式:one-hot与哑变量
- Activity、Window、View三者之间的联系
- myeclipse连接mysql生成数据表时中文字符乱码或问号(解决方法)
- 【BZOJ1426】收集邮票 期望DP
- python---使用字典来实现链接表图
- [转] 用webpack的CommonsChunkPlugin提取公共代码的3种方式
- Codeforces 382 D Permutations
- [微信小程序] 微信小程序下拉滚动选择器picker绑定数据的两种方式
- flask_admin 笔记三 客户化视图
- win10 添加项目右键用vscode打开
- Erlang TCP Socket的接收进程的2种方案
- 如何获取用户的地理位置? &;&; html5 地理位置
- pycharm/webstorm创建react项目
- C++父子类继承时的隐藏、覆盖、重载
热门文章
- 『学了就忘』Linux基础命令 — 28、别名和常用快捷键
- ansible模块及语法
- Invalid prop: type check failed for prop ";xxx";. Expected Number, got String.
- Intellij IDEA 配置Junit
- FZU ICPC 2020 寒假训练 3
- Redis的ACID属性
- CODING 项目协同 2.0 —— 让协作有条不紊
- 如何找到mysql磁盘地址
- jmeter ssh command方式执行hive指令
- [NOIP2017 提高组] 列队