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
Feature: Add a way to mock a constructor with defined values #209
Comments
I am facing the same requirement to retrieve the constructor arguments for the created class. It is good to see a feature request which is not closed immediately so it seems to be at least feasible. Hope to see it soon. |
I need this, too. |
I'm not sure, but if I read the readme correctly, the current implementation of This, of course, is not ideal for true power to the user. Ideally we could do something like this: // Note this class has two constructors
class Cls() {
constructor(a: Any?) : this()
}
@Test
fun `some test`() = mockkConstructors(Cls::class) {
val mock1 = mockk<Cls>()
val mock2 = mockk<Cls>()
val a = 0
every { Cls() } returns mock1
every { Cls(a) } returns mock2
} Notes:
|
Yes, you are right. The underlying mocking stub is only one. And all the calls from created objects via Returning arbitrary object/mocks as a result of the constructor calls is not possible. But attaching different stubs is possible. I've made an attempt to implement a variety of constructors in a simple way: #267 I have an idea that |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. If you are sure that this issue is important and should not be marked as |
@Stale[bot] come on |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. If you are sure that this issue is important and should not be marked as |
We need this! It will be really useful to test constructors with parameters |
can i asked why this was closed? This would be really nice functionality that has a real use cases. Stop closing this ticket bot |
Check #356 |
I would like this too. |
In relation to this, when mocking constructors that are not the default constructor ie they have parameters the mocking doesn't appear to work and instead the real code gets called. I think that adding the ability to mock constructors individually would fix this issue. |
bump - would like to see this too |
+1 for this feature |
+1 for this as well |
+1 the current implementation of constructor mocking is not enough. |
I am facing the same issue while mocking the class with parameter constructor.
|
I tried to write this but apparently it's not possible I haven't used MockK a lot yet but expected it to work like that. |
+1 for this feature. |
I am also very interested in this. |
+1 |
+1 The workaround I use (in case I own the code) is to define a factory method with |
+1 |
10 similar comments
+1 |
+1 |
+1 |
+1 |
+1 |
+1 |
+1 |
+1 |
+1 |
+1 |
+1! |
1 similar comment
+1! |
+1 |
2 similar comments
+1 |
+1 |
Has this partially/entirely been fixed in v1.11.0? |
Yep, v1.11.0, just released, adds |
Prerequisites
Please answer the following questions for yourself before submitting an issue.
Expected Behavior
Actually we can mock constructors but there is no way to handle constructors parameters. Something like:
For reference there is a question on SO about that.
The text was updated successfully, but these errors were encountered: