Skip to content

Commit

Permalink
[MERGE] API Unit Test 관련 파일들 추가 (#195)
Browse files Browse the repository at this point in the history
  • Loading branch information
ffalswo2 authored Nov 28, 2023
2 parents c195733 + 42d32c3 commit 9876065
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 2 deletions.
100 changes: 100 additions & 0 deletions LionHeart-iOS/LionHeart-iOS.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@
B59BFD3F2ADBBF2B005D2D81 /* CurriculumFactory.swift in Sources */ = {isa = PBXBuildFile; fileRef = B59BFD3E2ADBBF2B005D2D81 /* CurriculumFactory.swift */; };
B59BFD412ADBBFB2005D2D81 /* MyPageFactory.swift in Sources */ = {isa = PBXBuildFile; fileRef = B59BFD402ADBBFB2005D2D81 /* MyPageFactory.swift */; };
B59BFD432ADBBFF5005D2D81 /* ArticleFactory.swift in Sources */ = {isa = PBXBuildFile; fileRef = B59BFD422ADBBFF5005D2D81 /* ArticleFactory.swift */; };
B5BE51C12B15B8F100042EF3 /* ServiceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5BE51C02B15B8F100042EF3 /* ServiceTests.swift */; };
B5BE51C72B15C75F00042EF3 /* URLSessionStub.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5BE51C62B15C75F00042EF3 /* URLSessionStub.swift */; };
B5BE51C92B15CB9600042EF3 /* JSONLoader.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5BE51C82B15CB9600042EF3 /* JSONLoader.swift */; };
B5C6A2B22A5DB0B10021BE5E /* ArticleDetailTableView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5C6A2B12A5DB0B10021BE5E /* ArticleDetailTableView.swift */; };
B5C6A2B42A5DB11A0021BE5E /* ThumnailTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5C6A2B32A5DB11A0021BE5E /* ThumnailTableViewCell.swift */; };
B5C6A2B62A5DD5FE0021BE5E /* TitleTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5C6A2B52A5DD5FE0021BE5E /* TitleTableViewCell.swift */; };
Expand Down Expand Up @@ -476,6 +479,9 @@
B59BFD3E2ADBBF2B005D2D81 /* CurriculumFactory.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CurriculumFactory.swift; sourceTree = "<group>"; };
B59BFD402ADBBFB2005D2D81 /* MyPageFactory.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MyPageFactory.swift; sourceTree = "<group>"; };
B59BFD422ADBBFF5005D2D81 /* ArticleFactory.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ArticleFactory.swift; sourceTree = "<group>"; };
B5BE51C02B15B8F100042EF3 /* ServiceTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ServiceTests.swift; sourceTree = "<group>"; };
B5BE51C62B15C75F00042EF3 /* URLSessionStub.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = URLSessionStub.swift; sourceTree = "<group>"; };
B5BE51C82B15CB9600042EF3 /* JSONLoader.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JSONLoader.swift; sourceTree = "<group>"; };
B5C6A2B12A5DB0B10021BE5E /* ArticleDetailTableView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ArticleDetailTableView.swift; sourceTree = "<group>"; };
B5C6A2B32A5DB11A0021BE5E /* ThumnailTableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ThumnailTableViewCell.swift; sourceTree = "<group>"; };
B5C6A2B52A5DD5FE0021BE5E /* TitleTableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TitleTableViewCell.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -920,6 +926,9 @@
B532E8472A5525C800F0DB19 /* LionHeart-iOSTests */ = {
isa = PBXGroup;
children = (
B5BE51C32B15BD6200042EF3 /* URLSessionStub */,
B5BE51BB2B15B7B100042EF3 /* Today */,
B5BE51BA2B15B79C00042EF3 /* Challenge */,
B532E8482A5525C800F0DB19 /* LionHeart_iOSTests.swift */,
);
path = "LionHeart-iOSTests";
Expand Down Expand Up @@ -1428,6 +1437,94 @@
path = Font;
sourceTree = "<group>";
};
B5BE51BA2B15B79C00042EF3 /* Challenge */ = {
isa = PBXGroup;
children = (
B5BE51C52B15BDBD00042EF3 /* JSON */,
B5BE51BC2B15B80F00042EF3 /* ViewModelStub */,
B5BE51BF2B15B8C300042EF3 /* ServiceTests */,
B5BE51C22B15B8F600042EF3 /* ViewControllerTests */,
);
path = Challenge;
sourceTree = "<group>";
};
B5BE51BB2B15B7B100042EF3 /* Today */ = {
isa = PBXGroup;
children = (
B5BE51CD2B15CC5300042EF3 /* JSON */,
B5BE51CF2B15CC5B00042EF3 /* ServiceTests */,
B5BE51CE2B15CC5700042EF3 /* ViewModelStub */,
B5BE51D02B15CC5F00042EF3 /* ViewControllerTests */,
);
path = Today;
sourceTree = "<group>";
};
B5BE51BC2B15B80F00042EF3 /* ViewModelStub */ = {
isa = PBXGroup;
children = (
);
path = ViewModelStub;
sourceTree = "<group>";
};
B5BE51BF2B15B8C300042EF3 /* ServiceTests */ = {
isa = PBXGroup;
children = (
B5BE51C02B15B8F100042EF3 /* ServiceTests.swift */,
);
path = ServiceTests;
sourceTree = "<group>";
};
B5BE51C22B15B8F600042EF3 /* ViewControllerTests */ = {
isa = PBXGroup;
children = (
);
path = ViewControllerTests;
sourceTree = "<group>";
};
B5BE51C32B15BD6200042EF3 /* URLSessionStub */ = {
isa = PBXGroup;
children = (
B5BE51C82B15CB9600042EF3 /* JSONLoader.swift */,
B5BE51C62B15C75F00042EF3 /* URLSessionStub.swift */,
);
path = URLSessionStub;
sourceTree = "<group>";
};
B5BE51C52B15BDBD00042EF3 /* JSON */ = {
isa = PBXGroup;
children = (
);
path = JSON;
sourceTree = "<group>";
};
B5BE51CD2B15CC5300042EF3 /* JSON */ = {
isa = PBXGroup;
children = (
);
path = JSON;
sourceTree = "<group>";
};
B5BE51CE2B15CC5700042EF3 /* ViewModelStub */ = {
isa = PBXGroup;
children = (
);
path = ViewModelStub;
sourceTree = "<group>";
};
B5BE51CF2B15CC5B00042EF3 /* ServiceTests */ = {
isa = PBXGroup;
children = (
);
path = ServiceTests;
sourceTree = "<group>";
};
B5BE51D02B15CC5F00042EF3 /* ViewControllerTests */ = {
isa = PBXGroup;
children = (
);
path = ViewControllerTests;
sourceTree = "<group>";
};
B5C6A2C32A5EF4AC0021BE5E /* DTO */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -2490,7 +2587,10 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
B5BE51C92B15CB9600042EF3 /* JSONLoader.swift in Sources */,
C034EDE02ADE3A4A00AD6FF3 /* LionHeart_iOSTests.swift in Sources */,
B5BE51C12B15B8F100042EF3 /* ServiceTests.swift in Sources */,
B5BE51C72B15C75F00042EF3 /* URLSessionStub.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
15 changes: 14 additions & 1 deletion LionHeart-iOS/LionHeart-iOS/Network/Base/APIService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,22 @@ protocol Requestable {
func request<T: Decodable>(_ request: URLRequest) async throws -> T?
}

protocol LHURLSession {
func data(for request: URLRequest) async throws -> (Data, URLResponse)
}

extension URLSession: LHURLSession {}

final class APIService: Requestable {

private let session: LHURLSession

init(session: LHURLSession = URLSession.shared) {
self.session = session
}

func request<T: Decodable>(_ request: URLRequest) async throws -> T? {
let (data, _) = try await URLSession.shared.data(for: request)
let (data, _) = try await session.data(for: request)
let decoder = JSONDecoder()
guard let decodedData = try? decoder.decode(BaseResponse<T>.self, from: data) else {
throw NetworkError.jsonDecodingError
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ final class CurriculumListWeekViewModelImpl: CurriculumListWeekViewModel {
} else {
promise(.success(BookmarkCompleted.delete.message))
}
// promise(.success(BookmarkCompleted.success.message))
} catch {
promise(.failure(error as! NetworkError))
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// ServiceTests.swift
// LionHeart-iOSTests
//
// Created by 김민재 on 11/28/23.
//

import XCTest

final class ServiceTests: XCTestCase {

override func setUpWithError() throws {
// Put setup code here. This method is called before the invocation of each test method in the class.
}

override func tearDownWithError() throws {
// Put teardown code here. This method is called after the invocation of each test method in the class.
}

func testExample() throws {
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct results.
// Any test you write for XCTest can be annotated as throws and async.
// Mark your test throws to produce an unexpected failure when your test encounters an uncaught error.
// Mark your test async to allow awaiting for asynchronous code to complete. Check the results with assertions afterwards.
}

func testPerformanceExample() throws {
// This is an example of a performance test case.
self.measure {
// Put the code you want to measure the time of here.
}
}

}
19 changes: 19 additions & 0 deletions LionHeart-iOS/LionHeart-iOSTests/URLSessionStub/JSONLoader.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// JSONLoader.swift
// LionHeart-iOSTests
//
// Created by 김민재 on 11/28/23.
//

import Foundation

final class JSONLoader {

func load(fileName: String) -> URL {
let bundle = Bundle(for: Self.self)
guard let fileURL = bundle.url(forResource: fileName, withExtension: "json") else {
return URL(fileURLWithPath: "")
}
return fileURL
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// URLSessionStub.swift
// LionHeart-iOSTests
//
// Created by 김민재 on 11/28/23.
//

import Foundation
@testable import LionHeart_iOS

final class URLSessionStub: LHURLSession {

private var data: Data?

init(data: Data? = nil) {
self.data = data
}

func data(for request: URLRequest) async throws -> (Data, URLResponse) {
return (data ?? Data(), URLResponse())
}
}

0 comments on commit 9876065

Please sign in to comment.