Skip to content

Commit

Permalink
[Test] #181 - Add MockObjects
Browse files Browse the repository at this point in the history
  • Loading branch information
clxxrlove committed Dec 5, 2024
1 parent 7747fea commit 7bd2558
Show file tree
Hide file tree
Showing 5 changed files with 210 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// MockAVPlayerManager.swift
// DataModuleTests
//
// Created by 정지용 on 12/5/24.
//

import Foundation
@testable import DataModule

final class MockAVPlayerManager: AVPlayerManager {
struct CallCount {
var play = 0
var pause = 0
var setVolume = 0
var setupAudioSession = 0
}

var callCount = CallCount()
var isPlaying: Bool = false

func play(url: URL) async {
callCount.play += 1
}

func pause() {
callCount.pause += 1
}

func setVolume(_ volume: Float) {
callCount.setVolume += 1
}

func setupAudioSession() throws {
callCount.setupAudioSession += 1
}
}
61 changes: 61 additions & 0 deletions Heim/DataModule/DataModuleTests/TestDouble/MockDataStorage.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//
// MockDataStorage.swift
// DataModuleTests
//
// Created by 정지용 on 12/5/24.
//
// swiftlint:disable force_cast

@testable import DataModule
@testable import Domain

final class MockDataStorage: DataStorage {
struct CallCount {
var readData = 0
var readDataWithTwoParameters = 0
var readAll = 0
var saveData = 0
var saveDataWithThreeParameters = 0
var deleteData = 0
var deleteAll = 0
}

struct Return {
var readData: Any!
var readAll: Any!
}

var callCount = CallCount()
var returnValue = Return()

func readData<T>(directory: String, fileName: String) async throws -> T where T: Decodable {
callCount.readDataWithTwoParameters += 1
return returnValue.readData as! T
}

func readData<T>(calendarDate: CalendarDate) async throws -> T where T: Decodable {
callCount.readData += 1
return returnValue.readData as! T
}

func readAll<T>(directory: String) async throws -> T where T: Decodable {
callCount.readAll += 1
return returnValue.readAll as! T
}

func saveData<T>(directory: String, fileName: String, data: T) async throws where T: Encodable {
callCount.saveDataWithThreeParameters += 1
}

func saveData<T>(calendarDate: CalendarDate, data: T) async throws where T: Encodable {
callCount.saveData += 1
}

func deleteData(calendarDate: CalendarDate) async throws {
callCount.deleteData += 1
}

func deleteAll() async throws {
callCount.deleteAll += 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// MockNetworkProvider.swift
// DataModuleTests
//
// Created by 정지용 on 12/5/24.
//
// swiftlint:disable force_cast

import Foundation
@testable import DataModule

final class MockNetworkProvider: NetworkProvider {
struct CallCount {
var request = 0
var makeURL = 0
}

struct Return {
var request: Any!
var makeURL: URL!
}

var callCount = CallCount()
var returnValue = Return()

func request<T>(target: any DataModule.RequestTarget, type: T.Type) async throws -> T where T: Decodable {
callCount.request += 1
return returnValue.request as! T
}

func makeURL(target: any DataModule.RequestTarget) throws -> URL? {
callCount.makeURL += 1
return returnValue.makeURL
}
}
34 changes: 34 additions & 0 deletions Heim/DataModule/DataModuleTests/TestDouble/MockRequestTarget.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//
// MockRequestTarget.swift
// DataModuleTests
//
// Created by 정지용 on 12/5/24.
//

import Foundation
@testable import DataModule

final class MockRequestTarget: RequestTarget {
var baseURL: String
var path: String
var method: HTTPMethod
var headers: [String: String]
var body: (any Encodable)?
var query: [String: Any]

init(
baseURL: String,
path: String,
method: HTTPMethod,
headers: [String: String],
body: (any Encodable)? = nil,
query: [String: Any]
) {
self.baseURL = baseURL
self.path = path
self.method = method
self.headers = headers
self.body = body
self.query = query
}
}
43 changes: 43 additions & 0 deletions Heim/DataModule/DataModuleTests/TestDouble/MockTokenManager.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//
// MockTokenManager.swift
// DataModuleTests
//
// Created by 정지용 on 12/5/24.
//

@testable import DataModule

final class MockTokenManager: TokenManager {
struct CallCount {
var isAccessTokenValid = 0
var loadAccessToken = 0
var loadRefreshToken = 0
var storeTokens = 0
}

struct Return {
var loadAccessToken: String!
var loadRefreshToken: String!
}

var callCount = CallCount()
var returnValue = Return()

func isAccessTokenValid() throws {
callCount.isAccessTokenValid += 1
}

func loadAccessToken() throws -> String {
callCount.loadAccessToken += 1
return returnValue.loadAccessToken
}

func loadRefreshToken() throws -> String {
callCount.loadRefreshToken += 1
return returnValue.loadRefreshToken
}

func storeTokens(accessToken: String, refreshToken: String, expiresIn: Int) throws {
callCount.storeTokens += 1
}
}

0 comments on commit 7bd2558

Please sign in to comment.