Swift unit tests mock class static method

Viewed 7699

I have a static method in class

class A {
    static func myStaticMethod() -> B {
        return B()
    }
}

class B {
    func getTitle() -> String {
        // some functionality here
    }
}

In my class method that i want to test i use it like:

func someBoolFunc() -> Bool {
    var b = A.myStaticMethod()
    if (b.getTitle() = “hello”) {
         return true
    }
    return false
}

How to write mock class for this... I tried:

class MockA: A {
    var myTitle:String
    // this seems incorrect, because i didn't override static function
    func myStaticMethod() -> MockB {
        var b = MockB()
        b.title = myTitle
        return b
    }
}

class MockB: B {
    var myTitle:String    

    func getTitle() -> String {

        return myTitle
    }
}

And in tests i wanted to use something like:

func testExample() {
    A = MockA
    MockA.title = "My title"
    // And now this func should use my MockA instead of A
    someBoolFunc()
}

But of course it is only in theory :(

1 Answers
Related