- 1 php为什么对象也能调出静态方法,php – 为什么静态方法是不可测的?
- 2 android利用lrucache存储图片的demo
- 3 商业智能(BI)的未来:探索式分析 协作式分析
- 4 用keyword实现Editor.GetSelection的退出功能
- 5 ardupilot_gazebo仿真(三)
- 6 Android通过DeepLink方式跳转其他App传递参数
- 7 java只保留整数_java 除法运算只保留整数位的4种方式
- 8 mysql表、函数等被锁住无响应的问题
- 9 我为什么对12000薪资Say “NO”!
- 10 mac怎么用终端写C语言,【新手提问】有知道用mac终端编c语言的网络编程的人吗?...
静态方法本身不是不可测试的,但如果被测试的对象调用静态方法,那么测试不能“进入”,而是调用存根方法.如果正在测试的对象调用一个常规的方法,测试可以给它一个替代对象,该方法的存根实现.
一般来说,严格的依赖关系是可测试的,而依赖注入(google it)使代码更具可测试性.
例如,假设我们有一个静态方法getCurrentUser()被我们正在测试的类使用,如下所示
class PostModel {
//...
public function getRecentPosts() {
return $this->database->from('posts')
->where(array('user' => UserModel::getCurrentUser()))
->limit(10);
}
}
现在UserModel :: getCurrentUser()不能替换为存根方法.如果我们使用一个通过对象引用调用的常规方法,我们可以在我们的测试中传递一个替代的存根对象.
class PostModel {
private $userModel;
public function __construct($userModel) {
$this->userModel = $userModel;
}
//...
public function getRecentPosts() {
return $this->database->from('posts')
->where(array('user' => $this->userModel->getCurrentUser()))
->limit(10);
}
}