Skip to content
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

Impossible to make mock of inner object for spyk #795

Open
vrumvrumich opened this issue Mar 3, 2022 · 5 comments
Open

Impossible to make mock of inner object for spyk #795

vrumvrumich opened this issue Mar 3, 2022 · 5 comments
Labels

Comments

@vrumvrumich
Copy link

vrumvrumich commented Mar 3, 2022

I'm trying to test class with partial mocking, so I decide to use spyk

class SomeClassTest{
    private val someMock: SomeFieldType = mockk()
    private val testInstance = spyk(SomeClass()){
        every { someField } returns someMock 
    }

    @Test
    fun `test method`() {
        every { someMock.someMethod() } returns mockk()

        testInstance.someMethod()

        verify { someMock.someMethod() }
    }
}
class SomeClass() {
     val someField = object : SomeFieldType () {
        override fun overridedMethod(): Any {
            return something;
        }
    }

    override fun someMethod() = someField.someMethod()
}

And I need to verify that internally created instance of anonymous class call method.
But problem that someField is still not mocked and call original method of this class

@Raibaz
Copy link
Collaborator

Raibaz commented Mar 3, 2022

Since your inner object is being instantiated internally, you need to mock the SomeFieldType constructor to have it return a mock:

class SomeClassTest {

    @Test
    fun `test method`() {
        mockkConstructor(SomeFieldType::class)

        testInstance.someMethod()
        
        verify { anyConstructed<SomeFieldType>().someMethod() }
    }
}

Does this make sense?

@vrumvrumich
Copy link
Author

vrumvrumich commented Mar 3, 2022

@Raibaz

    @Test
    fun `test method`() {
        mockkConstructor(SomeFieldType::class)
        every { anyConstructed<SomeFieldType>().someMethod() } returns someMock 

        testInstance.someMethod()

        verify { anyConstructed<SomeFieldType>().someMethod() }
    }

still trying to call original someMethod() of someField
override fun someMethod() = someField.someMethod()

@dpotyralski
Copy link

dpotyralski commented Mar 7, 2022

@Raibaz I think I have a similar issue, please could you check the code below:

    @Test
    fun `test method`() {
        mockkConstructor(BaseClass::class)

        SimpleService().start()

        verify { anyConstructed<BaseClass>().updateFromBaseClass("A") }
    }
class SimpleService {

    fun start() {
        val baseClass = BaseClass()
        baseClass.updateFromBaseClass("A")
    }

}

Debug shows that mockkConstructor is there:
Screenshot 2022-03-07 at 12 58 05

but eventually get:

Verification failed: call 1 of 1: BaseClass(mockkConstructor<BaseClass>()).updateFromBaseClass(eq(A))) was not called

What am I missing here ? With given example wanted to check if particular functions was triggered on the mock object.
On mockk version: "1.10.6" all works as expected, however after this release it stopped.

@vrumvrumich
Copy link
Author

vrumvrumich commented Mar 7, 2022

@Raibaz solution with mockkConstructor(SomeFieldType::class) work only when I not creating anonymous class using object but just create an instance of SomeFieldType

@stale
Copy link

stale bot commented Jul 10, 2022

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 just ask to put an important label.

@stale stale bot added the stale label Jul 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants