Coder Social home page Coder Social logo

spyk broken for class hierarchies about mockk HOT 5 CLOSED

mockk avatar mockk commented on May 21, 2024
spyk broken for class hierarchies

from mockk.

Comments (5)

oleksiyp avatar oleksiyp commented on May 21, 2024

Thanks for reporting. Let me check this example.

from mockk.

oleksiyp avatar oleksiyp commented on May 21, 2024

Okay, so this to be fixed in 1.5.2 soon.

There is a few points mixed here:

  1. The correct way to create a spy is just spyk(SpyTest()) to copy everything from an object or spyk<SpyTest>() to call a default constructor (specifying different than object type type parameter looks like error prone. will check how to make it better)
  2. just Runs.apply{println("Intercepted")} just does not work. just Runs pretty synthetic construct which actually do nothing on response. println is executed but at declaration time, not at interception time. Is it confusing?
  3. There is a bug in spy initialization code that doesn't take into account base classes (fixed, thanks for pointing out)

Besides fixing a bug I will check spyk type parameter and introduce way to intercept spy calls(this actually a bug as well):

        every {
            spyObj.doLog()
        } answers {
            println("Intercepted");
            callOriginal();
            nothing
        }

Documentation update would be nice to do as well.

from mockk.

oleksiyp avatar oleksiyp commented on May 21, 2024

@dan-van can you check 1.5.2 version?

Something alike this should work:

        open class BaseTest(val someReference:String) {
            private var logger: org.slf4j.Logger = LoggerFactory.getLogger(BaseTest::class.java)
            open fun doLog() {
                logger.info("Base $someReference")
            }
        }

        class SpyTest() : BaseTest("A spy"){
            private var logger: org.slf4j.Logger = LoggerFactory.getLogger(SpyTest::class.java)
            override fun doLog() {
                logger.info("spyTest")
            }
        }

        val spyObj = spyk(SpyTest())
        every {
            spyObj.doLog()
        } answers {
            println("Intercepted");
            callOriginal();
        }

        assert(spyObj.someReference != null)
        spyObj.doLog()

from mockk.

oleksiyp avatar oleksiyp commented on May 21, 2024

Looks like issue is resolved. @dan-van Thanks for reporting again

from mockk.

dan-van avatar dan-van commented on May 21, 2024

Many thanks - sorry I was "away from project" for a few days. Yes 1.5.3 works for my usecase.

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.