-
-
Notifications
You must be signed in to change notification settings - Fork 367
Allow suppressing "Failed to set backing field" OR fail test #1291
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Hi, I also got the same problem today. @cgm-aw : Could you elaborate how you circumvented this issue using https://mockk.io/#property-backing-fields? Maybe you could just provide the adjusted test code. My current attempts do not work, unfortunately. |
Hi @bjpe yes I added it to the description. Use this: I also noticed that formatting in my post was a mess, I fixed that. |
Hi @cgm-aw , thank you very much! I also tried this, but it failed in my case because the field is not accessible (the mocked class is located in a library). Anyway, thanks a lot! |
I have a similar issue. I'm in Android, using val rect = mockk<RectF>(relaxed = true) {
every { width() } returns 1600f
every { height() } returns 900f
every { left } returns 0f // error: Missing mocked calls inside every { ... } block: make sure the object inside the block is a mock
every { getProperty("left") } propertyType java.lang.Float::class answers { 0f } // error: can't find property left for dynamic property get
} |
I'm not really happy with the solution - in my mind, this is a bug because the developer wanted to mock something which just does not work. I added a PR with a proposal for a fix: #1349 |
Introduction
Hi everyone,
I encountered a behavior of mockk, where, if the backing field has a different type than the getter, a warning is printed to the logs but the test continues and passes.
This seems weird to me - I would either expect the test to fail or no warning message.
My suggestion:
Add a parameter to the mockk() function to be able to suppress the warning, and if suppression is not active, the test will fail (or the other way around).
I'm aware of this: https://mockk.io/#property-backing-fields
With the solution presented there, everything works fine. However, if you do not use this mechanism, you will just get confusing error messages and probably an inconsistent behavior.
Prerequisites
Expected Behavior
Either a broken test or no log message
Current Behavior
A warning message is printed, including a stack trace, but the test passes
Steps to Reproduce
Create this class (this is a Java class!)
Create this Kotlin test:
You will see this log entry:
Using propertyType yields the correct behavior:
every { testObject.someField } propertyType Optional::class answers { "mockValue" }
Context
Stack trace
Minimal reproducible code (the gist of this issue)
See above
The text was updated successfully, but these errors were encountered: