我想测试我的WordPress应用程序,该应用程序调用此函数https://github.com/Automattic/WPCOM-Related-Posts/blob/master/wpcom-related-posts.php
我不确定如何模仿,因为WPCom_Related_Posts没有命名空间。
这就是我使用类
的方法DateTime jodaCurrentDate = new DateTime();
DateTimeFormatter formatter = DateTimeFormat.forPattern("YYYY-MM-dd HH:mm");
String startingDate = formatter.print(jodaCurrentDate.minusYears(2));
String endingDate = formatter.print(jodaCurrentDate);
"DATE BETWEEN TO_DATE (\'"+ startingDate +"\',\'YYYY-MM-DD HH24:MI\') AND " +
"TO_DATE (\'"+ endingDate +"\',\'YYYY-MM-DD HH24:MI\')";
我应该如何在测试中模拟这个scaleMode= .Fill
,因为这个类会调用Elasticsearch来获取相关的帖子,我不想在PHPUnit中设置它
答案 0 :(得分:1)
这很简单:就像你嘲笑任何其他类一样:
// As above but handle coercing of the files parameter types
this.project.metaClass.invokeMethod = { String name, args ->
if (name == 'exec') {
// Call special instance to track verifications
mySpecialInstance.exec((Closure) args.first())
} else {
// This calls the delegate without causing infinite recursion
// https://stackoverflow.com/a/10126006/1509221
MetaMethod metaMethod = delegate.class.metaClass.getMetaMethod(name, args)
logInvokeMethod(name, args, metaMethod)
// Special case 'files' method which can throw exceptions
if (name == 'files') {
// Coerce the arguments to match the signature of Project.files(Object... paths)
// TODO: is there a way to do this automatically, e.g. coerceArgumentsToClasses?
assert 0 == args.size() || 1 == args.size()
if (args.size() == 0 || // files()
args.first() == null) { // files(null)
return metaMethod?.invoke(delegate, [[] as Object[]] as Object[])
} else {
// files(ArrayList) possibly, so cast ArrayList to Object[]
return metaMethod?.invoke(delegate, [(Object[]) args.first()] as Object[])
}
} else {
// Normal pass through
return metaMethod?.invoke(delegate, args)
}
}
}
在第一页上有很多关于此事的documentation along with examples,例如:
$mock = Mockery::mock('WPCom_Related_Posts');
//or, if you want to be explicit:
$mocl = Mockery::mock('\WPCom_Related_Posts');
那应该回答你的问题($mock = \Mockery::mock('stdClass');
也没有被命名空间(好吧,它在全局NS中)。
不要忘记在每个测试结束时运行stdClass
(在\Mockery::close();
或tearDown
中,或者在您正在使用的测试框架中调用的任何内容) 。特别是当你开始使用期望时