有没有办法编写 SKPhysicsContactDelegate 函数的测试?

Is there a way to write a test of a `SKPhysicsContactDelegate` functions?

我可以模拟 SKPhysicsContact 对象以输入 -(void)didEndContact:(SKPhysicsContact *)contact 方法吗?或者是否有任何其他技术可以在这里利用?

class PhysicsTestCase: XCTestCase {

    var physics: GamePhysics!

    ...

    func testCaseOfCollisionsHandling() {

        let contact = SKPhysicsContact()
        contact.bodyA = SKPhysicsBody(circleOfRadius: 10) // Error, 'bodyA' is get-only property

        physics.didEnd(contact) // Physics conforms to `SKPhysicsContactDelegate`
    }

    ...

}

...

// The class that is being tested

class GamePhysics: NSObject, SKPhysicsContactDelegate {

    // MARK: - SKPhysicsContactDelegate

    func didBegin(_ contact: SKPhysicsContact)  {

        guard let nodeA = contact.bodyA.node, let nodeB = contact.bodyB.node else {
            fatalError("No nodes in colliding bodies")
        }

        switch (nodeB, nodeA) {

        case let (ball as LogicalBall, tile as LogicalTile):
           // Performing some logic

        ...

        }
    }

    func didEnd(_ contact: SKPhysicsContact) {

        ...
    }

    ...
}

当我们因为不拥有 API 而无法更改类型时,解决方案是使用遗留代码技术子类和覆盖方法:

class TestablePhysicsContact: SKPhysicsContact {
    var stubbedBodyA: SKPhysicsBody?

    override var bodyA: SKPhysicsBody {
        return stubbedBodyA!
    }
}

要在您的示例测试中使用它:

    func testCaseOfCollisionsHandling() {
        let contact = TestablePhysicsContact()
        contact.stubbedBodyA = SKPhysicsBody(circleOfRadius: 10)

        physics.didEnd(contact)

        // assert something
    }

有关此技术的更多信息,请参阅 https://qualitycoding.org/swift-partial-mock/

尽管 Jon Reid 在 中提出的 subclassing 非常巧妙,但由于 SKPhysicsContact class本身。

解决这个问题的方法是使用旧的 Objective C 运行时:

func testBallsCollisionIsPassedToHandler() {

    let ballAMock = LogicalBallMock()
    let bodyA = SKPhysicsBody(circleOfRadius: 10)
    bodyA.perform(Selector(("setRepresentedObject:")), with: ballAMock) // So the bodyA.node will return ballAMock

    let ballBMock = LogicalBallMock()
    let bodyB = SKPhysicsBody(circleOfRadius: 10)
    bodyB.perform(Selector(("setRepresentedObject:")), with: ballBMock) // So the bodyB.node will return ballBMock

    let contact = SKPhysicsContact()
    contact.perform(Selector(("setBodyA:")), with: bodyA)
    contact.perform(Selector(("setBodyB:")), with: bodyB)

    physics.didEnd(contact)

    // Assertions ...       

}