Xcode 单元测试 - 无法在单元测试运行时使打印命令一致显示

时间:2021-03-30 23:01:45

标签: swift xcode unit-testing xcode12

我最近开始在 Xcode 12.4 版中对基于 SwiftUI 的应用程序运行单元测试。

当我运行测试时,我无法理解输出中实际显示了哪些打印命令,哪些没有。

例如,我有一个用于测试的 MockRepository 类,而不是调用 Firebase 的普通存储库,并且我在 loadData 方法中有两个打印命令。有一段时间,它们都没有出现在输出中。现在我可以使用第一个命令来显示输出(非常一致),但第二个命令没有显示任何内容。

我已尝试在该行添加断点,但仍然没有看到输出。我还从正在测试的实际类中的几个方法中打印出打印命令,但没有从其他辅助方法中打印出来。

是否有一种好方法可以一致地判断哪些打印命令会出现在输出中,哪些不会(用于帮助调试)?

这是我的带有 loadData 函数的模拟存储库:

//
//  MockActionRepository.swift
//  GoalTogetherTests
//
//  Created by Charlie Page on 3/11/21.
//

import Foundation
@testable import GoalTogether

class MockActionRepository: ObservableObject, ActionStoreType {

    var db = TestDatabase()
    @Published var actions: [Action] = [Action]()
    var actionsPublished: Published<[Action]> { _actions }
    var actionsPublisher: Published<[Action]>.Publisher { $actions }
    
    init() {
        loadData()
    }
    
    func loadData() {
        let actionsList = db.actionsCollection
        self.actions = actionsList
        
        print("Here's a new printout I'm going to print")
        print("The actions list third action of: \(self.actions[2].title) has a startDate of \(self.actions[2].startDate)")
    }
    
    func addAction(_ action: Action) {
        self.actions.append(action)
    }
    
    func updateAction(_ action: Action) {
        var actionList = self.actions
        let index = actionList.firstIndex(where: {$0.id == action.id})
        
        guard index != nil else {
            print("Could not find action to update")
            return
        }
        actionList[index!] = action
        
        self.actions = actionList
    }
    
    func deleteAction(_ action: Action) {
        var actionList = self.actions
        
        if let index = actionList.firstIndex(where: {$0.id == action.id}) {
            actionList.remove(at: index)
        }
        self.actions = actionList
    }
}

这里是调用它的测试方法:

//
//  ActionListViewModelTests.swift
//  GoalTogetherTests
//
//  Created by Charlie Page on 3/11/21.
//

import XCTest
@testable import GoalTogether

class ActionListViewModelTests: XCTestCase {

    var sut: ActionListViewModel!
    
    override func setUp() {
        super.setUp()
        sut = ActionListViewModel(actionRepository: MockActionRepository())
    }
    
    override func tearDown() {
        sut = nil
        super.tearDown()
    }
    
    func testActionListVM_loadData() {
        // given
        let count = sut.actionRepository.actions.count
        
        // when
        
        // assert
        XCTAssertEqual(count, 3)
    }
    
    func testActionListVM_addAction_canAdd() {
        // given
        let testAction = Action(title: "Test Action specific to the canAdd test.")
        
        // when
        sut.addAction(testAction)
        
        // assert
        XCTAssertTrue(sut.actionRepository.actions.contains(where: { $0.title == testAction.title}))
    }
    
    func testActionListVM_updateAction_canUpdate() {
        // given
        var updatedAction = sut.actionRepository.actions[0]
        
        // when
        updatedAction.title = "New updated action title"
        sut.updateAction(updatedAction)
        
        // assert
        XCTAssertTrue(sut.actionRepository.actions[0].title == "New updated action title")
    }

    func testActionListVM_removeAction_canRemove() {
        // given
        let removalAction = sut.actionRepository.actions[1]
        
        // when
        sut.deleteAction(removalAction)
        let count = sut.actionRepository.actions.count
        
        XCTAssertEqual(count, 2)
    }
    
    func testActionListVM_loadPastActions_hasPastAction() {
        // given
        let pastCount = sut.previousActionCellViewModels.count
        
        // when
        
        // assert
        XCTAssertEqual(pastCount, 1)
    }
    
    func testActionListVM_loadBaseDateActions_hasBaseDateAction() {
        // given
        let currentCount = sut.baseDateActionCellViewModels.count
        
        // when
        
        // assert
        XCTAssertEqual(currentCount, 2)
    }
    
}

这是我的输出的第一部分(我选择了“所有输出”),它提取了一些输出,但不是全部:

User ID is: Optional("EHOohLd6jMPuLp7CpxtajvYjj2t2")
The current date of 2021-03-30 22:52:12 +0000 is not the end of the week (2021-04-03 22:52:12 +0000)
Start date is 2021-03-31 05:00:00 +0000 and the end of that week is 2021-04-03 05:00:00 +0000
Test Suite 'All tests' started at 2021-03-30 17:52:12.662
Test Suite 'GoalTogetherTests.xctest' started at 2021-03-30 17:52:12.663
Test Suite 'ActionListViewModelTests' started at 2021-03-30 17:52:12.663
Test Case '-[GoalTogetherTests.ActionListViewModelTests testActionListVM_addAction_canAdd]' started.
Here's a new printout I'm going to print
The current date of 2021-03-30 22:52:25 +0000 is not the end of the week (2021-04-03 22:52:25 +0000)
Start date is 2021-03-31 05:00:00 +0000 and the end of that week is 2021-04-03 05:00:00 +0000
Test Case '-[GoalTogetherTests.ActionListViewModelTests testActionListVM_addAction_canAdd]' passed (12.570 seconds).
Test Case '-[GoalTogetherTests.ActionListViewModelTests testActionListVM_loadBaseDateActions_hasBaseDateAction]' started.
Here's a new printout I'm going to print
The current date of 2021-03-30 22:52:25 +0000 is not the end of the week (2021-04-03 22:52:25 +0000)
Start date is 2021-03-31 05:00:00 +0000 and the end of that week is 2021-04-03 05:00:00 +0000
Test Case '-[GoalTogetherTests.ActionListViewModelTests testActionListVM_loadBaseDateActions_hasBaseDateAction]' passed (0.172 seconds).

0 个答案:

没有答案
相关问题