Code Monkey home page Code Monkey logo

Comments (2)

seanhenry avatar seanhenry commented on May 24, 2024

Hi @BergQuester

Thanks for raising this issue. Static mocks are a little more complicated because it will be very easy to accidentally keep state between tests. And I think, no matter what we do, static methods cannot be mocked when doing parallel testing.

That said, I can see how the missing support for static methods is irritating since they won't compile and require code to be added manually after each generation. So perhaps with documentation of their restrictions we can add this feature.

I think the main challenge is to easily reset mock state between tests so I've come up with the following:

protocol MyProtocol {
    static func a()
}

class MyProtocolMock: MyProtocol {

    class Static {
        var invokedA = false
        var invokedACount = 0
    }

    static var `static` = Static()

    static func a() {
        MyProtocolMock.static.invokedA = true
        MyProtocolMock.static.invokedACount += 1
    }
}

class MyClass {
    static func a() {} // cannot be overridden
    class func b() {}
}

class MyClassMock: MyClass {

    class Static {
        var invokedB = false
        var invokedBCount = 0
    }

    static var `static` = Static()

    override class func b() {
        MyClassMock.static.invokedB = true
        MyClassMock.static.invokedBCount += 1
    }
}

class MyTest: XCTestCase {

    var mock: MyProtocolMock!

    override func setUp() {
        super.setUp()
        mock = MyProtocolMock()
        MyProtocolMock.static = .init()
    }
}

What do you think?

from swiftmockgeneratorforxcode.

seanhenry avatar seanhenry commented on May 24, 2024

Closed due to inactivity

from swiftmockgeneratorforxcode.

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.