Comments (9)
This and other ...By
are analogous to returnsBy
, which accepts () -> T
on purpose, so it's possible to use property/function reference.
from mokkery.
The two are not mutually exclusive
from mokkery.
I am not sure what you mean. Could you elaborate?
from mokkery.
You can have an overloaded version that is more like calls. To get around this I have defined a function like this:
infix fun <T> SuspendAnsweringScope<Result<T>>.returnsSuccessBy(
block: (CallArgs) -> T,
) {
calls {
try { success(block(it)) } catch (t: Throwable) { failure(t) }
}
}
from mokkery.
I don't really like adding this overload:
- It's ambiguous. To resolve it, you're forced to use a function reference or specify the lambda parameter explicitly.
- The distinction between
returns..By
andcalls
becomes less clear. I see...By
answers as simple utilities for working with properties (especially) or functions without accessingCallArgs
.
However, I do see a blind spot in the API - a missing utility that simplifies working with results while still providing full access to the mocked method context. To address this gap, I propose:
public infix fun <T> BlockingAnsweringScope<Result<T>>.callsCatching(block: BlockingCallDefinitionScope<Result<T>>.(CallArgs) -> T)
public infix fun <T> SuspendAnsweringScope<Result<T>>.callsCatching(block: suspend SuspendCallDefinitionScope<Result<T>>.(CallArgs) -> T)
- It has stronger name association with
calls
. - Clear intent - the name also relates to
runCatching
, clearly explaining what's happening. - It provides access to other mocked method context operations.
from mokkery.
Works for me. Awaiting this feature. I was trying to implement it myself, but I don't think it is possible outside the library.
from mokkery.
You can temporarily add this:
public infix fun <T> BlockingAnsweringScope<Result<T>>.callsCatching(block: BlockingCallDefinitionScope<Result<T>>.(CallArgs) -> T) {
calls { runCatching { block(it) } }
}
It's not perfect as the Answer.description
returns calls {...}
instead of callsCatching {...}
. It should not be a problem as this description is used only by debug utils. If it's problematic, implementing custom answers is fully possible (since 2.2.0
it's possible to instantiate BlockingCallDefinitionScope
and SuspendCallDefinitionScope
).
from mokkery.
You can temporarily add this:
public infix fun <T> BlockingAnsweringScope<Result<T>>.callsCatching(block: BlockingCallDefinitionScope<Result<T>>.(CallArgs) -> T) { calls { runCatching { block(it) } } }It's not perfect as the
Answer.description
returnscalls {...}
instead ofcallsCatching {...}
. It should not be a problem as this description is used only by debug utils. If it's problematic, implementing custom answers is fully possible (since2.2.0
it's possible to instantiateBlockingCallDefinitionScope
andSuspendCallDefinitionScope
).
That doesn't quite work as you want block to be of type BlockingCallDefinitionScope<T>.(CallArgs) -> T
not BlockingCallDefinitionScope<Result<T>>.(CallArgs) -> T
, i..e. it shoud return T
not Result<T>
from mokkery.
It is correct. The T
in CallDefinitionScope<T>
indicates the return type of a mocked method, so it being Result<T>
is correct. The only inconvenience is that callOriginal
and callSuper
are returning Result
that requires unwrapping.
Also, it got me thinking about the other issue. callOriginal
and callSuper
might fail and probably MokkeryRuntimeException
s should not be caught with callsCatching
from mokkery.
Related Issues (20)
- Kotlin 2.0.0 support HOT 8
- Mocks incompatible with value classes HOT 3
- Is not possible to mock a `data class` HOT 3
- Mocking `abstract`/`open` classes requires default constructor HOT 2
- Wasm-WASI Coroutines support HOT 1
- Kotlin 2.0.20 HOT 1
- NotImplementedError Error thrown during migration HOT 4
- NoSuchElementException thrown during migration HOT 2
- NullPointerException thrown during migration HOT 3
- Provide a short hand way to define every/everySuspend with all arg matchers set to any() HOT 2
- Mocked interface doesn't return the value already set with every HOT 4
- Provide way to return a computed value not just a straight value with returns/returnsSuccess HOT 2
- MockMode.autoUnit does not work when mocking functional types HOT 1
- Provide a way to block the return from a mocked suspend function HOT 13
- Not able to verify objects by their content HOT 2
- no such method: ContextIndependentParameterRenderer HOT 3
- Mokkery & all-open plugin on WasmJs HOT 6
- Shorthand for adding mokkery-coroutines does not work HOT 3
- Provide callback mechanism when calls are made HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mokkery.