在php中使用callable(use of callable in php)
我试图在下面的实现中模仿php的内置usort函数定义:
class heapSort { static function hsort(array &$array, callable $cmp_function){ // logic } } class utility{ static function mycomparator(){ // logic } } $array = array(5,3,8,1); $callback = array('utility','mycomparator'); heapSort::hsort($array, $callback);
虽然变量
$callback
是“可调用的”,但为什么我会遇到致命错误?传递给heapSort :: hsort()的参数2必须是可调用的实例。
更具体地说,如何使
$variable
可以调用/类型化?I am trying to mimic php's inbuilt usort function definition in my implementation below:
class heapSort { static function hsort(array &$array, callable $cmp_function){ // logic } } class utility{ static function mycomparator(){ // logic } } $array = array(5,3,8,1); $callback = array('utility','mycomparator'); heapSort::hsort($array, $callback);
While the variable
$callback
is "callable" why do I get below fatal error?Argument 2 passed to heapSort::hsort() must be an instance of callable.
More specifically, how do I make/typecast a
$variable
to callable?
原文:https://stackoverflow.com/questions/15818301
最满意答案
getBicycle()方法永远不会返回RuntimeException。 代码本身正在捕获RuntimeException,并在被捕获时写入记录器。 方法本身将返回Bicycle或null。
您需要重新考虑getBicycle方法的运行方式。 如果您希望RuntimeException通过,它可能会重新抛出RuntimeException atfer logging。 但是,根据编写的方式,RuntmeException永远不会进入JUnit测试
The getBicycle() method will never return a RuntimeException. The code itself is catching the RuntimeException and, when caught, writes to the logger. The method itself will either return the Bicycle or null.
You will need to rethink how you want the getBicycle method operates. It could re-throw the RuntimeException atfer logging if you want the RuntimeException to bubble through. But, based on how that's written, the RuntmeException will never make it out to the JUnit test
相关问答
更多-
为什么它不起作用。 那么你的问题在于你的测试中的TransactionTemplate是一个模拟。 因此它具有与TransactionTemplate相同的接口,但它不知道如何表现。 你负责它的实现 - 这就是嘲笑的重点。 您在代码中明确调用了template.execute() ,这就是您的第一次验证通过的原因。 但是execute()并不是Spring的一个(或者更准确地说,在你的测试中template不是Spring的TransactionTemplate的一个实例,它只是它的一个模拟) - 也就是说 ...
-
您可以在testcase中创建新的内部类,扩展EmployeeRegistry,并覆盖方法“findEmployeeById”并调用新类的“createEmployee”。 -- Answer posted below I learnt that i don't actually need to spy, i can just mock the internal method as well and that works :) e.g., @Test public void createEmployeeTh ...
-
您需要先模拟包含selectSomethingBySomething()的类并记录此行为。 在mockito中你会说: SomeDao someDaoMock = mock(SomeDao.class); willThrow(new SQLException())).given(someDaoMock).selectSomethingBySomething(); 然后在测试中注入一些someDaoMock到你的类中,当它调用一些someDaoMock.selectSomethingBySomething ...
-
你的问题实际上是两个: 1:我如何避免执行模拟方法? 2:我如何模拟static方法? 这里是答案: Mockito提供了2个API来配置你的模拟。 更常见的(也是更可读的)是: when( mock.mockedMethod() ) .thenReturn(someValue); 问题在于真正的方法实际上是被执行的,只是结果被替换了。 通常这不是问题,除非您的方法基于同一对象中其他(模拟的但可能未配置的)方法的返回值抛出(未经检查的)异常,或者尝试访问模拟对象的依赖关系上的方法,因为它们为null所以一 ...
-
我终于找到了解决方案:我的第一个测试是一个Kotlin,在一个“公开课”中,但是是一个未打开的方法声明。 Mockito 不能用包访问或最终方法来嘲笑一种方法 ! 在这种特殊情况下,当你试图嘲笑它时,它就会调用真正的方法,而不会告诉你任何事情...... 也许这篇文章对别人有用! - I finally ended finding the solution : My first test was a Kotlin one, in a "open class" but a non-opened method ...
-
我不知道你的className,你需要将你的className替换为你自己的className。 也许,你可以做这样的事情,只是检查方法真正调用方法。 @Mock private INotificationDao iNotificationDao; private ClassName className; @Before public void init() { className = spy(new ClassName()); } @Test public vo ...
-
如何强制Mockito在JUnit测试中抛出RemoteException(How to force Mockito to throw RemoteException in JUnit test)[2022-10-22]
getBicycle()方法永远不会返回RuntimeException。 代码本身正在捕获RuntimeException,并在被捕获时写入记录器。 方法本身将返回Bicycle或null。 您需要重新考虑getBicycle方法的运行方式。 如果您希望RuntimeException通过,它可能会重新抛出RuntimeException atfer logging。 但是,根据编写的方式,RuntmeException永远不会进入JUnit测试 The getBicycle() method will ... -
我在这里为mapper提供了一个示例测试课程。 减速机的测试也可以用相同的方式编写。 @RunWith(MockitoJUnitRunner.class) public class SampleMapperTest { @Mock private Mapper.Context mockContext; // declare your mocks @Mock Counter mockCounter; // mocked hadoop counter SampleM ...
-
我使用了以下方法: 我的TestClass扩展了BaseServiceTestCase(在liferay src中可用) 将portal-test.properties保留在test文件夹中(带有测试值)。 运行测试用例。 在这种情况下,liferay会加载所有的属性以及弹簧初始化。 I used the following method : My TestClass extends BaseServiceTestCase (available in liferay src) Keep portal-tes ...
-
那么,代码可以改进,使其实际可测试... 目前,没有很好的方法为代码编写单元测试,因为构造函数创建了一个JasperserverRestClient而没有任何改变它的机会。 您可以做的最少是添加另一个构造函数(可能是包访问)以允许使用另一个JasperserverRestClient。 (或者你可以考虑使用Factory模式。但这可能会很复杂。) 然后你可以嘲笑...... JasperserverRestClient jasperServerClient = Mockito.mock( Jasperser ...