好吧,所以我被推荐使用 XCTests,但它完全让我困惑。所以我尝试在其中初始化一个小测试。我已经编辑了程序的名称,因为我不确定在线隐私如何运作。
// -----Tests.swift
// -----Tests
import XCTest
final class -----Tests: XCTestCase {
func testCallMoveFunctionForPlayer() throws {
// Create a Board instance
let board = Board()
// Create a Mob instance
let mob = Mob()
// Define the start point
let startPoint = Coord(row: 0, col: 0)
// Place the Mob at the start point on the Board
board.pieces[startPoint] = mob
// Define the destination
let destination = Coord(row: 1, col: 1)
// Call the function to test
board.callMoveFunctionForPlayer(tapRow: destination.row, tapCol: destination.col, startPoint: startPoint, piece: mob)
// Check if the Mob has moved to the destination
XCTAssertNil(board.pieces[startPoint])
XCTAssertNotNil(board.pieces[destination])
XCTAssertEqual(board.pieces[destination], mob)
}
}
但是,我被告知 Mob 和 Board 都不在范围内。所以我将该文件移到我的应用程序文件旁边,但它仍然不起作用,然后我将其移回来,但现在我有一个不同的问题“没有像'XCTest'这样的模块”,这对我来说没有意义,我将它移动到它来自的同一个文件中。
我希望有一种简单的方法来自动检查游戏中一些最常见的错误,但它不起作用。我尝试一路撤消,但即使在其原始状态,它仍然声称该模块不存在。我没有删除任何文件或其他任何东西,我只是来回移动它。我什么也没改变。
XCTests 在单独的应用程序目标中运行。这本质上是一个单独的应用程序。您不希望测试代码与主应用程序分开,因此您可以将其分开,并知道它不会意外(或不利)影响任何事情。这意味着您需要将主要代码公开给测试目标。这是通过使用
@testable
导入您的应用程序来完成的
将其添加到测试文件的顶部
import XCTest
@testable import YourAppName // <--- Add This
final class Tests: XCTestCase {
func testCallMoveFunctionForPlayer() throws {
// Test code