如何在swift的单元测试中验证是否在具体类中调用了方法

如何在swift的单元测试中验证是否在具体类中调用了方法,swift,unit-testing,Swift,Unit Testing,我有这样一个场景: class X { init(y: ProtocolA) func foo(){ if(y.isSomething()){ methodA() } else { methodB() } } func methodA(){ // any } func methodB(){ // any }

我有这样一个场景:

class X {
    init(y: ProtocolA)

    func foo(){
        if(y.isSomething()){
            methodA()
        } else {
            methodB()
        }
    }

    func methodA(){
        // any 
    }

    func methodB(){
        // any
    }

}

class Y : ProtocolA {

    func isSomething(): Bool { return true OR false }

}
我想考X班

我将模拟ProtocolA在两个不同的测试中返回isSomething()方法true或false,以了解是否调用了方法a方法b

解决这个问题的最佳策略是什么

ps:对于mockito,将间谍与验证一起使用是非常容易的,但是对于Swift,这是非常痛苦的

编辑:

嗯,我这样做:

第一部分:

class X { init(y: Y) }
protocol Y { func isSomething() -> Bool }
现在,测试的结构:mockspy对象

typealias VerifyMethodAssert = (count: Int, parameters: [Any]?, returnn: Any?)
可配置的依赖项模拟

class YMock : Y {


        init(configure: Bool)
        func isSomething{ return configure }

    }
混凝土类间谍

class XSpy : X {

    private let y: Y

    var verify: [String: VerifyMethodAssert] = [
        "methodA()": (count: 0, parameters: nil, returnn: nil)
        "methodB()": (count: 0, parameters: nil, returnn: nil)
    ]

    var nothing: [String: Bool] = [
        "methodA()": false
        "methodB()": false
    ]

    init(y: Y, verify: [String: VerifyMethodAssert]?, nothing: [String: Bool]?)

    func methodA(){
        verify["\(#function)"] = (count: verify["\(#function)"]!.count + 1, parameters: nil, 
            returnn: nothing["\(#function)"]! ? nil : super.methodA())
    }

    func methodB(doNothing: Bool = false){
        verify["\(#function)"] = (count: verify["\(#function)"]!.count + 1, parameters: nil, 
            returnn: nothing["\(#function)"]! ? nil : super.methodB())
    }

}
和测试:

class XTest : QuickSpec {

    override func spec(){
        describe("a position view model"){

            it("test 1"){
                let y = Y(configure: true)
                let x = XSpy(y: y)

                x.foo()

                expect(1).to(x.verify["methodA()"].count)
                expect(0).to(x.verify["methodB()"].count)
            }

            it("test 2"){
                let y = Y(configure: true)
                let x = XSpy(y: y)

                x.foo()

                expect(0).to(x.verify["methodA()"].count)
                expect(1).to(x.verify["methodB()"].count)
            }

        }
    }
}

据我所知,没有现成的方法。 一种方法是检查计数器:

class X {

    var countA: Int = 0
    var countB: Int = 0

    init(y: ProtocolA)

    func foo(){
        if(y.isSomething()){
            methodA()
        } else {
            methodB()
        }
    }

    func methodA(){
        countA += 1
        // any 

    }

    func methodB(){
        countB += 1
        // any
    }

}

也建议使用这种方法。

在这种特定情况下,您可以将
X子类化,并使用关联的对象来保存调用计数,如果您看到自己反复使用它,则可以对其进行概括:

final class TestX: X {

    private struct AssociatedKeys {
        static var invocations = "\(String(describing: type(of: TestX.self)))-invocations"
    }

    func invocations(for method: String) -> Int {
        return invocations[method] ?? 0
    }

    private var invocations: [String: Int] {
        get {
            return objc_getAssociatedObject(self, &AssociatedKeys.invocations) as? [String: Int] ?? [:]
        }
        set {
            objc_setAssociatedObject( self, &AssociatedKeys.invocations, newValue as NSDictionary, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }

    override func methodA(){
        super.methodA()
        invocations["methodA"] = (invocations["methodA"] ?? 0) + 1
    }

    override func methodB(){
        super.methodB()
        invocations["methodB"] = (invocations["methodB"] ?? 0) + 1
    }
}

let x = TestX(y: Y())
x.invocations(for: "methodA") //0
x.foo()
x.invocations(for: "methodA") //1