Can I mock SKPhysicsContact
object to feed into -(void)didEndContact:(SKPhysicsContact *)contact
method? 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) {
...
}
...
}
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.The way this problem can be solved is to use good old Objective C runtime: