I am trying to use mockk anyConstructed but ending with Missing calls inside every { ... } block

Viewed 397

I am trying to return an object whenever a new object of a class is being created.

I have tried using anyConstructed with a spyk or even mockk object of PredictionCheckFunction

every { anyConstructed<PredictionCheckFunction>() } answers { predictionCheckFunction }

It results in the following error on the above line

io.mockk.MockKException: Missing calls inside every { ... } block.

In Mockito I would do something like this

whenNew(PredictionCheckFunction.class).withNoArguments().thenReturn(predictionCheckFunction);

I want to make sure that every creation of PredictionCheckFunction results in an object of predictionCheckFunction

The example in this Question How to mock a new object using mockk allows me to only run a function on a mock object but not return one already created like above Mockito example thenReturn(predictionCheckFunction); -

Example in referred SO Question -

mockkConstructor(Dog::class)
every { anyConstructed<Dog>().bark() }

Any help on how to do this while creation of a new object, is appreciated.

0 Answers
Related