在测试中的视图模型中初始化模拟问题 类 - swift

Issue initializing mocks in a view model in testing classes - swift

我正在尝试设置一个视图模型以使用各种模拟存储库进行测试,但在我的测试中加载模拟时遇到问题 class - 我在我的主视图模型中创建了一个初始化程序这应该包括三个存储库,如果没有 selected,则将它们设置为实际存储库,但允许我 select 在我的测试中符合每个存储库协议的不同模拟 class .

当我加载我的测试 class 时,我 运行 遇到了这个问题,它说我的两个存储库的变量不在范围内。知道什么可能导致这种错误吗?

class SeasonGoalsViewModel {
    @Published var goalRepository: GoalStoreType
    @Published var seasonRepository: SeasonStoreType?
    @Published var seasonLength: SeasonLength?
    
    var userProfile: UserProfile
    
    init(goalRepository: GoalStoreType = GoalRepository(), seasonRepository: SeasonStoreType? = SeasonRepository(), userProfile: UserProfile? = CurrentUserProfile.shared.currentUser) {
        self.goalRepository = goalRepository
        self.seasonRepository = seasonRepository
        self.userProfile = userProfile!
    }
}
import XCTest
@testable import MyApp

class SeasonGoalsViewModelTests: XCTestCase {
    
    var sut: SeasonGoalsViewModel!
    
    override func setUp() {
        super.setUp()
        sut = SeasonGoalsViewModel(goalRepository: MockGoalsRepository(), seasonRepository = MockSeasonRepository(), userProfile = TestUserProfile.shared.userProfile)
    }

我在 seasonRepository 上遇到错误,说“在范围内找不到 'seasonRepository',在 userProfile 上,说“在范围内找不到 'userProfile'”。

在创建 sut 对象时,在 setUp() 中将 = 替换为 :

import XCTest
    @testable import MyApp
    
    class SeasonGoalsViewModelTests: XCTestCase {
        
        var sut: SeasonGoalsViewModel!
        
        override func setUp() {
            super.setUp()
            sut = SeasonGoalsViewModel(goalRepository: MockGoalsRepository(), seasonRepository: MockSeasonRepository(), userProfile: TestUserProfile.shared.userProfile)
        }