代码之家  ›  专栏  ›  技术社区  ›  classyname

当我只来回移动文件时,为什么会得到“没有这样的模块‘XCTest’”?

  •  -1
  • classyname  · 技术社区  · 1 年前

    好的,所以我被推荐使用XCTests,但它完全让我困惑。所以我尝试在其中初始化一个小测试。我已经编辑了我的程序名称,因为我不确定隐私是如何在线工作的。

    // -----Tests.swift
    
    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’这样的模块”,这对我来说毫无意义,我把它移动到了它来自的同一个文件中。

    我希望有一种简单的方法来自动检查游戏中一些最常见的bug,但没有成功。我一直试图撤消,但即使在它的原始状态下,它仍然声称模块不存在。我没有删除任何文件或其他任何东西,我只是来回移动它。我什么都没改。

    1 回复  |  直到 1 年前
        1
  •  0
  •   ColdLogic    1 年前

    XCTest在单独的应用程序目标中运行。这本质上是一个独立的应用程序。你不希望你的测试代码与你的主应用程序分开,这样你就可以把它分开,并知道它不会意外地(或不利地)影响任何事情。这意味着您需要向测试目标公开您的主代码。这是通过使用导入您的应用程序来完成的 @testable

    将其添加到测试文件的顶部

    import XCTest 
    
    @testable import YourAppName // <--- Add This
    
    final class Tests: XCTestCase { 
    
        func testCallMoveFunctionForPlayer() throws {
            // Test code
    

    这会将主应用程序中的所有代码暴露给测试目标,而不会将测试代码暴露给主应用程序。允许您使用在测试中开发的所有代码。

    No such module as XCTest 错误,听起来您更改了测试文件的目标。在编辑器中打开测试文件,然后在右侧的检查器窗格中,在 Target Membership ,检查它是否只是测试目标的一部分,而不是主要目标的一个部分。