Skip to content

Commit

Permalink
Test: MockNetwork클래스와 UnitTest작성 (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
dahun-lee-daji committed Apr 28, 2021
1 parent 962f4a5 commit 0d849bf
Show file tree
Hide file tree
Showing 9 changed files with 314 additions and 26 deletions.
145 changes: 144 additions & 1 deletion SideDishApp/SideDishApp.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,27 @@
1ECF2EBC2635D6FD006982BE /* MenuRepository.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1ECF2EBB2635D6FD006982BE /* MenuRepository.swift */; };
1ECF2EC02635DE65006982BE /* CoreData.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = 1ECF2EBE2635DE65006982BE /* CoreData.xcdatamodeld */; };
1ECF2EC32635E2EC006982BE /* CoreDataMenuResponseStorage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1ECF2EC22635E2EC006982BE /* CoreDataMenuResponseStorage.swift */; };
B04466392637B17600DAD616 /* MockNetworkManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = B04466382637B17600DAD616 /* MockNetworkManager.swift */; };
B04466432638149900DAD616 /* SideDishAppTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B04466422638149900DAD616 /* SideDishAppTests.swift */; };
B044664D263814B400DAD616 /* NetworkTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B044664C263814B400DAD616 /* NetworkTests.swift */; };
B04466512638152200DAD616 /* testData.json in Resources */ = {isa = PBXBuildFile; fileRef = B04466502638152200DAD616 /* testData.json */; };
B044665D26383DD500DAD616 /* testData.json in Resources */ = {isa = PBXBuildFile; fileRef = B04466502638152200DAD616 /* testData.json */; };
B0911E6F262EA29200D5EBEC /* DishCardCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = B0911E6E262EA29200D5EBEC /* DishCardCell.swift */; };
B0911EA026316B9A00D5EBEC /* String+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = B0911E9F26316B9A00D5EBEC /* String+Extension.swift */; };
B0911EA42631AF3400D5EBEC /* DiffableProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = B0911EA32631AF3400D5EBEC /* DiffableProvider.swift */; };
D8279798DA0A1BCEB97E7780 /* Pods_SideDishApp.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 96A87B12151B0D006007CD63 /* Pods_SideDishApp.framework */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
B04466452638149900DAD616 /* PBXContainerItemProxy */ = {
isa = PBXContainerItemProxy;
containerPortal = 1E1DBAC6262DDDFC0050FE6C /* Project object */;
proxyType = 1;
remoteGlobalIDString = 1E1DBACD262DDDFC0050FE6C;
remoteInfo = SideDishApp;
};
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
1E1DBACE262DDDFC0050FE6C /* SideDishApp.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SideDishApp.app; sourceTree = BUILT_PRODUCTS_DIR; };
1E1DBAD1262DDDFC0050FE6C /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
Expand All @@ -53,6 +68,12 @@
4942C5A177086C5C99933038 /* Pods-SideDishApp.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-SideDishApp.debug.xcconfig"; path = "Target Support Files/Pods-SideDishApp/Pods-SideDishApp.debug.xcconfig"; sourceTree = "<group>"; };
605A37FD869863C7E023EE85 /* Pods-SideDishApp.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-SideDishApp.release.xcconfig"; path = "Target Support Files/Pods-SideDishApp/Pods-SideDishApp.release.xcconfig"; sourceTree = "<group>"; };
96A87B12151B0D006007CD63 /* Pods_SideDishApp.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_SideDishApp.framework; sourceTree = BUILT_PRODUCTS_DIR; };
B04466382637B17600DAD616 /* MockNetworkManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockNetworkManager.swift; sourceTree = "<group>"; };
B04466402638149900DAD616 /* SideDishAppTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = SideDishAppTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
B04466422638149900DAD616 /* SideDishAppTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SideDishAppTests.swift; sourceTree = "<group>"; };
B04466442638149900DAD616 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
B044664C263814B400DAD616 /* NetworkTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetworkTests.swift; sourceTree = "<group>"; };
B04466502638152200DAD616 /* testData.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = testData.json; sourceTree = "<group>"; };
B0911E6E262EA29200D5EBEC /* DishCardCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DishCardCell.swift; sourceTree = "<group>"; };
B0911E9F26316B9A00D5EBEC /* String+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "String+Extension.swift"; sourceTree = "<group>"; };
B0911EA32631AF3400D5EBEC /* DiffableProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DiffableProvider.swift; sourceTree = "<group>"; };
Expand All @@ -67,6 +88,13 @@
);
runOnlyForDeploymentPostprocessing = 0;
};
B044663D2638149900DAD616 /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
Expand All @@ -82,6 +110,7 @@
isa = PBXGroup;
children = (
1E1DBAD0262DDDFC0050FE6C /* SideDishApp */,
B04466412638149900DAD616 /* SideDishAppTests */,
1E1DBACF262DDDFC0050FE6C /* Products */,
25F4FE78497FA432687D2A6F /* Pods */,
134494D3AD3A3BAD38EFF6AF /* Frameworks */,
Expand All @@ -92,6 +121,7 @@
isa = PBXGroup;
children = (
1E1DBACE262DDDFC0050FE6C /* SideDishApp.app */,
B04466402638149900DAD616 /* SideDishAppTests.xctest */,
);
name = Products;
sourceTree = "<group>";
Expand Down Expand Up @@ -123,6 +153,7 @@
children = (
1ECD5F61262F256000DC56C0 /* Endpoint.swift */,
1ECD5F65262F2E1E00DC56C0 /* NetworkManager.swift */,
B04466382637B17600DAD616 /* MockNetworkManager.swift */,
1ECD5F69262F3B6500DC56C0 /* DishNetworkManager.swift */,
);
path = Network;
Expand Down Expand Up @@ -245,6 +276,17 @@
path = Pods;
sourceTree = "<group>";
};
B04466412638149900DAD616 /* SideDishAppTests */ = {
isa = PBXGroup;
children = (
B04466502638152200DAD616 /* testData.json */,
B04466422638149900DAD616 /* SideDishAppTests.swift */,
B04466442638149900DAD616 /* Info.plist */,
B044664C263814B400DAD616 /* NetworkTests.swift */,
);
path = SideDishAppTests;
sourceTree = "<group>";
};
B0911E9E26316B8700D5EBEC /* Extension */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -275,18 +317,40 @@
productReference = 1E1DBACE262DDDFC0050FE6C /* SideDishApp.app */;
productType = "com.apple.product-type.application";
};
B044663F2638149900DAD616 /* SideDishAppTests */ = {
isa = PBXNativeTarget;
buildConfigurationList = B04466472638149900DAD616 /* Build configuration list for PBXNativeTarget "SideDishAppTests" */;
buildPhases = (
B044663C2638149900DAD616 /* Sources */,
B044663D2638149900DAD616 /* Frameworks */,
B044663E2638149900DAD616 /* Resources */,
);
buildRules = (
);
dependencies = (
B04466462638149900DAD616 /* PBXTargetDependency */,
);
name = SideDishAppTests;
productName = SideDishAppTests;
productReference = B04466402638149900DAD616 /* SideDishAppTests.xctest */;
productType = "com.apple.product-type.bundle.unit-test";
};
/* End PBXNativeTarget section */

/* Begin PBXProject section */
1E1DBAC6262DDDFC0050FE6C /* Project object */ = {
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 1230;
LastSwiftUpdateCheck = 1240;
LastUpgradeCheck = 1230;
TargetAttributes = {
1E1DBACD262DDDFC0050FE6C = {
CreatedOnToolsVersion = 12.3;
};
B044663F2638149900DAD616 = {
CreatedOnToolsVersion = 12.4;
TestTargetID = 1E1DBACD262DDDFC0050FE6C;
};
};
};
buildConfigurationList = 1E1DBAC9262DDDFC0050FE6C /* Build configuration list for PBXProject "SideDishApp" */;
Expand All @@ -303,6 +367,7 @@
projectRoot = "";
targets = (
1E1DBACD262DDDFC0050FE6C /* SideDishApp */,
B044663F2638149900DAD616 /* SideDishAppTests */,
);
};
/* End PBXProject section */
Expand All @@ -314,11 +379,20 @@
files = (
1E1DBADE262DDDFD0050FE6C /* LaunchScreen.storyboard in Resources */,
1ECF2E812632A70F006982BE /* LoadingView.xib in Resources */,
B044665D26383DD500DAD616 /* testData.json in Resources */,
1E1DBADB262DDDFD0050FE6C /* Assets.xcassets in Resources */,
1E1DBAD9262DDDFC0050FE6C /* Main.storyboard in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
B044663E2638149900DAD616 /* Resources */ = {
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
B04466512638152200DAD616 /* testData.json in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXResourcesBuildPhase section */

/* Begin PBXShellScriptBuildPhase section */
Expand Down Expand Up @@ -382,13 +456,31 @@
B0911EA42631AF3400D5EBEC /* DiffableProvider.swift in Sources */,
B0911E6F262EA29200D5EBEC /* DishCardCell.swift in Sources */,
1E1DBAD4262DDDFC0050FE6C /* SceneDelegate.swift in Sources */,
B04466392637B17600DAD616 /* MockNetworkManager.swift in Sources */,
1ECF2E842632A862006982BE /* LoadingView.swift in Sources */,
1ECD5F73262F405100DC56C0 /* MenuListViewModel.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
B044663C2638149900DAD616 /* Sources */ = {
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
B044664D263814B400DAD616 /* NetworkTests.swift in Sources */,
B04466432638149900DAD616 /* SideDishAppTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXSourcesBuildPhase section */

/* Begin PBXTargetDependency section */
B04466462638149900DAD616 /* PBXTargetDependency */ = {
isa = PBXTargetDependency;
target = 1E1DBACD262DDDFC0050FE6C /* SideDishApp */;
targetProxy = B04466452638149900DAD616 /* PBXContainerItemProxy */;
};
/* End PBXTargetDependency section */

/* Begin PBXVariantGroup section */
1E1DBAD7262DDDFC0050FE6C /* Main.storyboard */ = {
isa = PBXVariantGroup;
Expand Down Expand Up @@ -563,6 +655,48 @@
};
name = Release;
};
B04466482638149900DAD616 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
BUNDLE_LOADER = "$(TEST_HOST)";
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_TEAM = PSH4C5839A;
INFOPLIST_FILE = SideDishAppTests/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 14.4;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
"@loader_path/Frameworks",
);
PRODUCT_BUNDLE_IDENTIFIER = "com.Dahun-Lee.SideDishAppTests";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/SideDishApp.app/SideDishApp";
};
name = Debug;
};
B04466492638149900DAD616 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
BUNDLE_LOADER = "$(TEST_HOST)";
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_TEAM = PSH4C5839A;
INFOPLIST_FILE = SideDishAppTests/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 14.4;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
"@loader_path/Frameworks",
);
PRODUCT_BUNDLE_IDENTIFIER = "com.Dahun-Lee.SideDishAppTests";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/SideDishApp.app/SideDishApp";
};
name = Release;
};
/* End XCBuildConfiguration section */

/* Begin XCConfigurationList section */
Expand All @@ -584,6 +718,15 @@
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
B04466472638149900DAD616 /* Build configuration list for PBXNativeTarget "SideDishAppTests" */ = {
isa = XCConfigurationList;
buildConfigurations = (
B04466482638149900DAD616 /* Debug */,
B04466492638149900DAD616 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */

/* Begin XCVersionGroup section */
Expand Down
20 changes: 0 additions & 20 deletions SideDishApp/SideDishApp/Application/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,6 @@ import CoreData

@main
class AppDelegate: UIResponder, UIApplicationDelegate {

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
return true
}

// MARK: UISceneSession Lifecycle

func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
// Called when a new scene session is being created.
// Use this method to select a configuration to create the new scene with.
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}

func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
// Called when the user discards a scene session.
// If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
// Use this method to release any resources that were specific to the discarded scenes, as they will not return.
}

// MARK: - Core Data stack
lazy var persistentContainer: NSPersistentContainer = {
/*
Expand Down
46 changes: 46 additions & 0 deletions SideDishApp/SideDishApp/Data/Network/MockNetworkManager.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//
// MockNetworkManager.swift
// SideDishApp
//
// Created by 이다훈 on 2021/04/27.
//

import Foundation
import Combine

protocol URLSessionProtocol {
func dataTaskPublisher(for url: URL) -> URLSession.DataTaskPublisher
}

extension URLSession : URLSessionProtocol{
}

struct MockNetworkSessionSuccess : URLSessionProtocol {
func dataTaskPublisher(for url: URL) -> URLSession.DataTaskPublisher {

return URLSession.shared.dataTaskPublisher(for: Bundle.main.url(forResource: "testData", withExtension: "json")!)
}
}

class MockNetworkManager : NetworkManager {
override func get<T>(url: URL?) -> AnyPublisher<T, NetworkError> where T : Decodable {

guard let myUrl = url else {
return Fail(error: NetworkError.urlError).eraseToAnyPublisher()
}

return session.dataTaskPublisher(for: myUrl)
.mapError { _ in NetworkError.networkConnection }
.flatMap { data, response -> AnyPublisher<T, NetworkError> in
return Just(data)
.decode(type: T.self, decoder: JSONDecoder())
.mapError { error in
print(error.localizedDescription)
return NetworkError.parsing }.eraseToAnyPublisher()
}.eraseToAnyPublisher()
}


}


10 changes: 7 additions & 3 deletions SideDishApp/SideDishApp/Data/Network/NetworkManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,19 @@ protocol NetworkManageable {

class NetworkManager: NetworkManageable {

let session: URLSessionProtocol

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

func get<T>(url: URL?) -> AnyPublisher<T, NetworkError> where T: Decodable {
guard let myUrl = url else {

return Fail(error: NetworkError.urlError).eraseToAnyPublisher()
}

return URLSession.shared.dataTaskPublisher(for: myUrl)
return session.dataTaskPublisher(for: myUrl)
.mapError { _ in NetworkError.networkConnection }

.flatMap { data, response -> AnyPublisher<T, NetworkError> in
guard let httpResponse = response as? HTTPURLResponse else {
return Fail(error: NetworkError.responseNil).eraseToAnyPublisher()
Expand Down
Loading

0 comments on commit 0d849bf

Please sign in to comment.