我可以模仿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) {
...
}
...
}
答案 0 :(得分:0)
当我们因为不拥有API而无法更改类型时,解决方案是使用遗留代码技术Subclass和Override Method:
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/
答案 1 :(得分:0)
虽然,Jon Reid在https://stackoverflow.com/a/44101485/482853中提出的子类化非常简洁,但由于SKPhysicsContact
类本身的难以理解的特性,我没有设法使它在这种特殊情况下起作用。
解决这个问题的方法是使用旧的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 ...
}