Skip to content

Commit 1411ad7

Browse files
committed
* Initial Setup for the Unit test.
1 parent 3b3061f commit 1411ad7

File tree

12 files changed

+364
-40
lines changed

12 files changed

+364
-40
lines changed

AppUnitTest.xcodeproj/project.pbxproj

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,18 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
1715CE282D45415300D5344F /* MockNetworkEngine.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1715CE272D45415300D5344F /* MockNetworkEngine.swift */; };
11+
1715CE2A2D4541C300D5344F /* CartManagerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1715CE292D4541C300D5344F /* CartManagerTests.swift */; };
12+
1715CE2C2D4541E800D5344F /* MockCartManagerDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1715CE2B2D4541E800D5344F /* MockCartManagerDelegate.swift */; };
13+
17DBDA802D45394100B21AED /* CartManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17DBDA7F2D45394100B21AED /* CartManager.swift */; };
14+
17DBDA832D45397E00B21AED /* NetworkEngine.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17DBDA822D45397E00B21AED /* NetworkEngine.swift */; };
15+
17DBDA852D4539C700B21AED /* ProductManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17DBDA842D4539C700B21AED /* ProductManager.swift */; };
16+
17DBDA872D453AF400B21AED /* APIRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17DBDA862D453AF400B21AED /* APIRequest.swift */; };
1017
17E77EF92D45388E00BFA1A2 /* AppUnitTestApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17E77EF82D45388E00BFA1A2 /* AppUnitTestApp.swift */; };
1118
17E77EFB2D45388E00BFA1A2 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17E77EFA2D45388E00BFA1A2 /* ContentView.swift */; };
1219
17E77EFD2D45389200BFA1A2 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 17E77EFC2D45389200BFA1A2 /* Assets.xcassets */; };
1320
17E77F002D45389200BFA1A2 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 17E77EFF2D45389200BFA1A2 /* Preview Assets.xcassets */; };
14-
17E77F0A2D45389200BFA1A2 /* AppUnitTestTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17E77F092D45389200BFA1A2 /* AppUnitTestTests.swift */; };
21+
17E77F0A2D45389200BFA1A2 /* ProductManagerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17E77F092D45389200BFA1A2 /* ProductManagerTests.swift */; };
1522
17E77F142D45389200BFA1A2 /* AppUnitTestUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17E77F132D45389200BFA1A2 /* AppUnitTestUITests.swift */; };
1623
17E77F162D45389200BFA1A2 /* AppUnitTestUITestsLaunchTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17E77F152D45389200BFA1A2 /* AppUnitTestUITestsLaunchTests.swift */; };
1724
/* End PBXBuildFile section */
@@ -34,13 +41,20 @@
3441
/* End PBXContainerItemProxy section */
3542

3643
/* Begin PBXFileReference section */
44+
1715CE272D45415300D5344F /* MockNetworkEngine.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockNetworkEngine.swift; sourceTree = "<group>"; };
45+
1715CE292D4541C300D5344F /* CartManagerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CartManagerTests.swift; sourceTree = "<group>"; };
46+
1715CE2B2D4541E800D5344F /* MockCartManagerDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockCartManagerDelegate.swift; sourceTree = "<group>"; };
47+
17DBDA7F2D45394100B21AED /* CartManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CartManager.swift; sourceTree = "<group>"; };
48+
17DBDA822D45397E00B21AED /* NetworkEngine.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetworkEngine.swift; sourceTree = "<group>"; };
49+
17DBDA842D4539C700B21AED /* ProductManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProductManager.swift; sourceTree = "<group>"; };
50+
17DBDA862D453AF400B21AED /* APIRequest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = APIRequest.swift; sourceTree = "<group>"; };
3751
17E77EF52D45388E00BFA1A2 /* AppUnitTest.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = AppUnitTest.app; sourceTree = BUILT_PRODUCTS_DIR; };
3852
17E77EF82D45388E00BFA1A2 /* AppUnitTestApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppUnitTestApp.swift; sourceTree = "<group>"; };
3953
17E77EFA2D45388E00BFA1A2 /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = "<group>"; };
4054
17E77EFC2D45389200BFA1A2 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
4155
17E77EFF2D45389200BFA1A2 /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = "<group>"; };
4256
17E77F052D45389200BFA1A2 /* AppUnitTestTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = AppUnitTestTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
43-
17E77F092D45389200BFA1A2 /* AppUnitTestTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppUnitTestTests.swift; sourceTree = "<group>"; };
57+
17E77F092D45389200BFA1A2 /* ProductManagerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProductManagerTests.swift; sourceTree = "<group>"; };
4458
17E77F0F2D45389200BFA1A2 /* AppUnitTestUITests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = AppUnitTestUITests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
4559
17E77F132D45389200BFA1A2 /* AppUnitTestUITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppUnitTestUITests.swift; sourceTree = "<group>"; };
4660
17E77F152D45389200BFA1A2 /* AppUnitTestUITestsLaunchTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppUnitTestUITestsLaunchTests.swift; sourceTree = "<group>"; };
@@ -71,6 +85,34 @@
7185
/* End PBXFrameworksBuildPhase section */
7286

7387
/* Begin PBXGroup section */
88+
1715CE262D45414700D5344F /* Mock */ = {
89+
isa = PBXGroup;
90+
children = (
91+
1715CE272D45415300D5344F /* MockNetworkEngine.swift */,
92+
1715CE2B2D4541E800D5344F /* MockCartManagerDelegate.swift */,
93+
);
94+
path = Mock;
95+
sourceTree = "<group>";
96+
};
97+
17DBDA7E2D45393200B21AED /* Cart */ = {
98+
isa = PBXGroup;
99+
children = (
100+
17DBDA812D45396700B21AED /* NetworkManager */,
101+
17DBDA7F2D45394100B21AED /* CartManager.swift */,
102+
17DBDA842D4539C700B21AED /* ProductManager.swift */,
103+
);
104+
path = Cart;
105+
sourceTree = "<group>";
106+
};
107+
17DBDA812D45396700B21AED /* NetworkManager */ = {
108+
isa = PBXGroup;
109+
children = (
110+
17DBDA822D45397E00B21AED /* NetworkEngine.swift */,
111+
17DBDA862D453AF400B21AED /* APIRequest.swift */,
112+
);
113+
path = NetworkManager;
114+
sourceTree = "<group>";
115+
};
74116
17E77EEC2D45388E00BFA1A2 = {
75117
isa = PBXGroup;
76118
children = (
@@ -96,6 +138,7 @@
96138
children = (
97139
17E77EF82D45388E00BFA1A2 /* AppUnitTestApp.swift */,
98140
17E77EFA2D45388E00BFA1A2 /* ContentView.swift */,
141+
17DBDA7E2D45393200B21AED /* Cart */,
99142
17E77EFC2D45389200BFA1A2 /* Assets.xcassets */,
100143
17E77EFE2D45389200BFA1A2 /* Preview Content */,
101144
);
@@ -113,7 +156,9 @@
113156
17E77F082D45389200BFA1A2 /* AppUnitTestTests */ = {
114157
isa = PBXGroup;
115158
children = (
116-
17E77F092D45389200BFA1A2 /* AppUnitTestTests.swift */,
159+
1715CE262D45414700D5344F /* Mock */,
160+
17E77F092D45389200BFA1A2 /* ProductManagerTests.swift */,
161+
1715CE292D4541C300D5344F /* CartManagerTests.swift */,
117162
);
118163
path = AppUnitTestTests;
119164
sourceTree = "<group>";
@@ -258,6 +303,10 @@
258303
buildActionMask = 2147483647;
259304
files = (
260305
17E77EFB2D45388E00BFA1A2 /* ContentView.swift in Sources */,
306+
17DBDA852D4539C700B21AED /* ProductManager.swift in Sources */,
307+
17DBDA802D45394100B21AED /* CartManager.swift in Sources */,
308+
17DBDA832D45397E00B21AED /* NetworkEngine.swift in Sources */,
309+
17DBDA872D453AF400B21AED /* APIRequest.swift in Sources */,
261310
17E77EF92D45388E00BFA1A2 /* AppUnitTestApp.swift in Sources */,
262311
);
263312
runOnlyForDeploymentPostprocessing = 0;
@@ -266,7 +315,10 @@
266315
isa = PBXSourcesBuildPhase;
267316
buildActionMask = 2147483647;
268317
files = (
269-
17E77F0A2D45389200BFA1A2 /* AppUnitTestTests.swift in Sources */,
318+
1715CE2A2D4541C300D5344F /* CartManagerTests.swift in Sources */,
319+
1715CE282D45415300D5344F /* MockNetworkEngine.swift in Sources */,
320+
1715CE2C2D4541E800D5344F /* MockCartManagerDelegate.swift in Sources */,
321+
17E77F0A2D45389200BFA1A2 /* ProductManagerTests.swift in Sources */,
270322
);
271323
runOnlyForDeploymentPostprocessing = 0;
272324
};
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Scheme
3+
LastUpgradeVersion = "1500"
4+
version = "1.7">
5+
<BuildAction
6+
parallelizeBuildables = "YES"
7+
buildImplicitDependencies = "YES">
8+
<BuildActionEntries>
9+
<BuildActionEntry
10+
buildForTesting = "YES"
11+
buildForRunning = "YES"
12+
buildForProfiling = "YES"
13+
buildForArchiving = "YES"
14+
buildForAnalyzing = "YES">
15+
<BuildableReference
16+
BuildableIdentifier = "primary"
17+
BlueprintIdentifier = "17E77EF42D45388E00BFA1A2"
18+
BuildableName = "AppUnitTest.app"
19+
BlueprintName = "AppUnitTest"
20+
ReferencedContainer = "container:AppUnitTest.xcodeproj">
21+
</BuildableReference>
22+
</BuildActionEntry>
23+
</BuildActionEntries>
24+
</BuildAction>
25+
<TestAction
26+
buildConfiguration = "Debug"
27+
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
28+
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
29+
shouldUseLaunchSchemeArgsEnv = "YES"
30+
shouldAutocreateTestPlan = "YES">
31+
<Testables>
32+
<TestableReference
33+
skipped = "NO"
34+
parallelizable = "YES">
35+
<BuildableReference
36+
BuildableIdentifier = "primary"
37+
BlueprintIdentifier = "17E77F042D45389200BFA1A2"
38+
BuildableName = "AppUnitTestTests.xctest"
39+
BlueprintName = "AppUnitTestTests"
40+
ReferencedContainer = "container:AppUnitTest.xcodeproj">
41+
</BuildableReference>
42+
</TestableReference>
43+
<TestableReference
44+
skipped = "YES"
45+
parallelizable = "YES">
46+
<BuildableReference
47+
BuildableIdentifier = "primary"
48+
BlueprintIdentifier = "17E77F0E2D45389200BFA1A2"
49+
BuildableName = "AppUnitTestUITests.xctest"
50+
BlueprintName = "AppUnitTestUITests"
51+
ReferencedContainer = "container:AppUnitTest.xcodeproj">
52+
</BuildableReference>
53+
</TestableReference>
54+
</Testables>
55+
</TestAction>
56+
<LaunchAction
57+
buildConfiguration = "Debug"
58+
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
59+
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
60+
launchStyle = "0"
61+
useCustomWorkingDirectory = "NO"
62+
ignoresPersistentStateOnLaunch = "NO"
63+
debugDocumentVersioning = "YES"
64+
debugServiceExtension = "internal"
65+
allowLocationSimulation = "YES">
66+
<BuildableProductRunnable
67+
runnableDebuggingMode = "0">
68+
<BuildableReference
69+
BuildableIdentifier = "primary"
70+
BlueprintIdentifier = "17E77EF42D45388E00BFA1A2"
71+
BuildableName = "AppUnitTest.app"
72+
BlueprintName = "AppUnitTest"
73+
ReferencedContainer = "container:AppUnitTest.xcodeproj">
74+
</BuildableReference>
75+
</BuildableProductRunnable>
76+
</LaunchAction>
77+
<ProfileAction
78+
buildConfiguration = "Release"
79+
shouldUseLaunchSchemeArgsEnv = "YES"
80+
savedToolIdentifier = ""
81+
useCustomWorkingDirectory = "NO"
82+
debugDocumentVersioning = "YES">
83+
<BuildableProductRunnable
84+
runnableDebuggingMode = "0">
85+
<BuildableReference
86+
BuildableIdentifier = "primary"
87+
BlueprintIdentifier = "17E77EF42D45388E00BFA1A2"
88+
BuildableName = "AppUnitTest.app"
89+
BlueprintName = "AppUnitTest"
90+
ReferencedContainer = "container:AppUnitTest.xcodeproj">
91+
</BuildableReference>
92+
</BuildableProductRunnable>
93+
</ProfileAction>
94+
<AnalyzeAction
95+
buildConfiguration = "Debug">
96+
</AnalyzeAction>
97+
<ArchiveAction
98+
buildConfiguration = "Release"
99+
revealArchiveInOrganizer = "YES">
100+
</ArchiveAction>
101+
</Scheme>

AppUnitTest.xcodeproj/xcuserdata/anand.xcuserdatad/xcschemes/xcschememanagement.plist

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,23 @@
1010
<integer>0</integer>
1111
</dict>
1212
</dict>
13+
<key>SuppressBuildableAutocreation</key>
14+
<dict>
15+
<key>17E77EF42D45388E00BFA1A2</key>
16+
<dict>
17+
<key>primary</key>
18+
<true/>
19+
</dict>
20+
<key>17E77F042D45389200BFA1A2</key>
21+
<dict>
22+
<key>primary</key>
23+
<true/>
24+
</dict>
25+
<key>17E77F0E2D45389200BFA1A2</key>
26+
<dict>
27+
<key>primary</key>
28+
<true/>
29+
</dict>
30+
</dict>
1331
</dict>
1432
</plist>

AppUnitTest/Cart/CartManager.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//
2+
// CartManager.swift
3+
// AppUnitTest
4+
//
5+
// Created by Anand Nimje on 25/01/25.
6+
//
7+
8+
protocol CartManagerDelegate: AnyObject {
9+
func didUpdateCart(with items: [String])
10+
}
11+
12+
class CartManager {
13+
weak var delegate: CartManagerDelegate?
14+
private var cartItems: [String] = []
15+
16+
func addItem(_ item: String) {
17+
cartItems.append(item)
18+
delegate?.didUpdateCart(with: cartItems)
19+
}
20+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//
2+
// APIRequest.swift
3+
// AppUnitTest
4+
//
5+
// Created by Anand Nimje on 25/01/25.
6+
//
7+
8+
import Foundation
9+
10+
struct APIRequest {
11+
let url: URL
12+
let method: String
13+
let headers: [String: String]?
14+
let body: Data?
15+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//
2+
// NetworkEngine.swift
3+
// AppUnitTest
4+
//
5+
// Created by Anand Nimje on 25/01/25.
6+
//
7+
8+
import Combine
9+
import Foundation
10+
11+
protocol NetworkRequestable {
12+
func fetch<T: Decodable>(_ request: APIRequest, responseType: T.Type) async throws -> T
13+
}
14+
15+
class NetworkEngine: NetworkRequestable {
16+
func fetch<T: Decodable>(_ request: APIRequest, responseType: T.Type) async throws -> T {
17+
var urlRequest = URLRequest(url: request.url)
18+
urlRequest.httpMethod = request.method
19+
urlRequest.allHTTPHeaderFields = request.headers
20+
urlRequest.httpBody = request.body
21+
22+
let (data, response) = try await URLSession.shared.data(for: urlRequest)
23+
guard let httpResponse = response as? HTTPURLResponse, (200...299).contains(httpResponse.statusCode) else {
24+
throw URLError(.badServerResponse)
25+
}
26+
return try JSONDecoder().decode(T.self, from: data)
27+
}
28+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//
2+
// ProductManager.swift
3+
// AppUnitTest
4+
//
5+
// Created by Anand Nimje on 25/01/25.
6+
//
7+
8+
import Foundation
9+
10+
class ProductManager {
11+
private let networkEngine: NetworkRequestable
12+
13+
init(networkEngine: NetworkRequestable) {
14+
self.networkEngine = networkEngine
15+
}
16+
17+
func fetchProducts() async throws -> [String] {
18+
let request = APIRequest(
19+
url: URL(string: "https://api.example.com/products")!,
20+
method: "GET",
21+
headers: nil,
22+
body: nil
23+
)
24+
return try await networkEngine.fetch(request, responseType: [String].self)
25+
}
26+
}

AppUnitTestTests/AppUnitTestTests.swift

Lines changed: 0 additions & 36 deletions
This file was deleted.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//
2+
// CartManagerTests.swift
3+
// AppUnitTestTests
4+
//
5+
// Created by Anand Nimje on 25/01/25.
6+
//
7+
8+
import XCTest
9+
@testable import AppUnitTest
10+
11+
final class CartManagerTests: XCTestCase {
12+
func testDelegateCalledOnAddItem() {
13+
let cartManager = CartManager()
14+
let mockDelegate = MockCartManagerDelegate()
15+
mockDelegate.expectation = expectation(description: "Delegate method called")
16+
cartManager.delegate = mockDelegate
17+
18+
// Adding test value here
19+
cartManager.addItem("TShirt XL Size")
20+
waitForExpectations(timeout: 1, handler: nil)
21+
22+
XCTAssertEqual(mockDelegate.updatedItems, ["TShirt XL Size"], "Value should be Equal.")
23+
}
24+
}

0 commit comments

Comments
 (0)