Coder Social home page Coder Social logo

Comments (15)

rafalwrzeszcz avatar rafalwrzeszcz commented on May 21, 2024 6

This is a HUGE blocker, making testing default functions if not impossible then at least horribly complex instead of straight-forward. It was reported over 3 years ago and not even a feedback available here. What the …?

from mockk.

zakhenry avatar zakhenry commented on May 21, 2024 4

quick update, a much cleaner option works with abstract classes:

import io.mockk.*

data class ItemData(val id: Int)

interface Database {
    fun insertItems(items: List<ItemData>)
    fun deleteItems(items: List<ItemData>)
    fun runTransaction(alsoDelete: Boolean) {
        insertItems(listOf(ItemData(id = 1), ItemData(id = 2), ItemData(id = 3)))
        if (alsoDelete)
            deleteItems(listOf(ItemData(id = 4), ItemData(id = 5), ItemData(id = 6)))
    }
}


fun main(args: Array<String>) {
//    val x = mockk<Database>() // this should work, but doesn't due to https://github.com/mockk/mockk/issues/64

    // instead we create an abstract class to work around the issue.
    abstract class DatabaseMock: Database
    val x = mockk<DatabaseMock>()
    
    // the members/methods of the interface need to be stubbed too:
    every { x.insertItems(any()) } just Runs
    every { x.deleteItems(any()) } just Runs

    every { x.runTransaction(any()) } answers { callOriginal() }
    x.runTransaction(alsoDelete = true)
    verify { x.insertItems(listOf(ItemData(id = 1), ItemData(id = 2), ItemData(id = 3))) }
}

from mockk.

ondrej-simon avatar ondrej-simon commented on May 21, 2024 3

This is a problem caused by Mockk not being able to reliably spy interfaces. The solution I am using is also by creating an in-place abstract class "implementing" the interface I want to test, but instead of mockk-ing it, I use spyk call on the abstract class, that way i do not need to declare the callOriginal() on methods I want to actually test.

from mockk.

aivinog1 avatar aivinog1 commented on May 21, 2024 1

Hello everyone! I was encountered with this too and I wish to fix it :) So, I can start to fix it but are there any clues or advice about it?

from mockk.

flapenna avatar flapenna commented on May 21, 2024 1

@flapenna : Is this issue fixed by above PR?

It should, but the fix hasn't been released yet

from mockk.

Raibaz avatar Raibaz commented on May 21, 2024 1

Sorry about the delay in the release, planning to make one next week.

from mockk.

fargus9 avatar fargus9 commented on May 21, 2024

This is blocking some of my tests as well...

from mockk.

jsingle avatar jsingle commented on May 21, 2024

Same here

from mockk.

stale avatar stale commented on May 21, 2024

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 put an important tag.

from mockk.

munnitz avatar munnitz commented on May 21, 2024

What's the status on this one? I tried using this when mocking an interface like so

class InterfaceMock : Interface by mockk() {
    init {
        coEvery { defaultSuspendFunctionCall(any()) } coAnswers { callOriginal() }
    }
}

but it does not seem to work. Am I using it incorrectly or is it still not fixed?

from mockk.

juniofernandes-hotmart avatar juniofernandes-hotmart commented on May 21, 2024

Same issue here. Any feedbacks?

from mockk.

dugsmith avatar dugsmith commented on May 21, 2024

I'm also blocked writing a test like this because of this problem. Any plans to fix it?

from mockk.

zakhenry avatar zakhenry commented on May 21, 2024

I've banged my head against this for a while and found an workaround. Annoying but workable in the interim:

import io.mockk.*

data class ItemData(val id: Int)

interface Database {
    fun insertItems(items: List<ItemData>)
    fun deleteItems(items: List<ItemData>)
    fun runTransaction(alsoDelete: Boolean) {
        insertItems(listOf(ItemData(id = 1), ItemData(id = 2), ItemData(id = 3)))
        if (alsoDelete)
            deleteItems(listOf(ItemData(id = 4), ItemData(id = 5), ItemData(id = 6)))
    }
}


fun main(args: Array<String>) {
//    val x = mockk<Database>() // this should work, but doesn't due to https://github.com/mockk/mockk/issues/64

    // instead we create an anonymous object that implements the interface (but doesn't actually implement it; we stub the methods)
    val x = spyk(object: Database {
        override fun deleteItems(items: List<ItemData>) {}
        override fun insertItems(items: List<ItemData>) {}
    })
    // the members/methods of the interface can be stubbed if needed: 
//    every { x.insertItems(any()) } just Runs
//    every { x.deleteItems(any()) } just Runs

    every { x.runTransaction(any()) } answers { callOriginal() }
    x.runTransaction(alsoDelete = true)
    verify { x.insertItems(listOf(ItemData(id = 1), ItemData(id = 2), ItemData(id = 3))) }
}

from mockk.

levinotik avatar levinotik commented on May 21, 2024

Ran into this as well today. I resolved with @zakhenry's workaround, but it'd be great if this could be fixed. Makes tests a bit ugly and confusing for others when they see this empty abstract class sitting there.

from mockk.

goro-7 avatar goro-7 commented on May 21, 2024

@flapenna : Is this issue fixed by above PR?

from mockk.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.