我可以模拟SKPhysicsContact
对象来喂入-(void)didEndContact:(SKPhysicsContact *)contact
方法吗?或者还有其他技术可以在这里使用吗?有没有办法编写一个`SKPhysicsContactDelegate`函数的测试?
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) {
...
}
...
}
您可以创建一个小而项目和彼此的顶部初始化你的对象。应该在这种情况下调用'didEndContact'。 –
你到底在做什么?你为什么要设置物理体?这不是你永远不会做的事情。 –
我想写一组测试,检查我的'SKPhysicsContactDelegate'是否按预期工作。 – Zapko