简体   繁体   English

有没有一种方法可以编写SKPhysicsContactDelegate函数的测试?

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

Can I mock SKPhysicsContact object to feed into -(void)didEndContact:(SKPhysicsContact *)contact method? 我可以模拟SKPhysicsContact对象以馈入-(void)didEndContact:(SKPhysicsContact *)contact方法吗? Or is there any other technique, that can be leveraged here? 还是在这里可以利用其他任何技术?

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) {

        ...
    }

    ...
}

When we can't change a type because we don't own the API, the solution is to use the legacy code technique Subclass and Override Method: 当我们由于不拥有API而无法更改类型时,解决方案是使用遗留代码技术Subclass和Override Method:

class TestablePhysicsContact: SKPhysicsContact {
    var stubbedBodyA: SKPhysicsBody?

    override var bodyA: SKPhysicsBody {
        return stubbedBodyA!
    }
}

To use this in your example test: 要在示例测试中使用它,请执行以下操作:

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

        physics.didEnd(contact)

        // assert something
    }

For more on this technique, see https://qualitycoding.org/swift-partial-mock/ 有关此技术的更多信息,请参见https://qualitycoding.org/swift-partial-mock/

Although, subclassing proposed by Jon Reid in https://stackoverflow.com/a/44101485/482853 is very neat, I didn't managed to make it work in this particular case because of elusive nature of SKPhysicsContact class itself. 虽然,乔恩·里德(Jon Reid)在https://stackoverflow.com/a/44101485/482853中提出的子类非常整洁,但由于SKPhysicsContact类本身的难以捉摸的特性,我未能使其在这种特殊情况下SKPhysicsContact

The way this problem can be solved is to use good old Objective C runtime: 解决此问题的方法是使用良好的旧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 ...       

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM