-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from MrMUsmanAli/feature/added_tests
added some unit tests
- Loading branch information
Showing
2 changed files
with
120 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,121 @@ | ||
import XCTest | ||
import Wiretapp | ||
|
||
final class wiretappTests: XCTestCase { | ||
func testExample() { | ||
XCTAssertTrue(1 == 1) | ||
struct User: Decodable { | ||
let id: Int | ||
let name: String | ||
let username: String | ||
let email: String | ||
let phone: String | ||
let address: Address | ||
|
||
struct Address: Decodable { | ||
let street: String | ||
let suite: String | ||
let city: String | ||
let zipcode: String | ||
} | ||
} | ||
struct Post: Decodable { | ||
let userId: Int | ||
let id: Int | ||
let title: String | ||
let body: String | ||
} | ||
|
||
func test_noEnvironmentVarsLoadsNormally() { | ||
Wiretapp.registerSharedSession() | ||
setenv(Wiretapp.recordEnabled, "false", 1) | ||
setenv(Wiretapp.testCaseName, "", 1) | ||
let expectation = XCTestExpectation(description: "outbound request made to web") | ||
|
||
URLSession.shared.dataTask( | ||
with: URL(string: "https://jsonplaceholder.typicode.com/users")! | ||
) { data, response, error in | ||
if | ||
let data = data, | ||
let decoded = try? JSONDecoder().decode([User].self, from: data), | ||
decoded.count > 1 | ||
{ | ||
expectation.fulfill() | ||
} | ||
} | ||
.resume() | ||
wait(for: [expectation], timeout: 10.0) | ||
} | ||
|
||
func test_usingTestCaseNameInvokesMockResponses() { | ||
Wiretapp.registerSharedSession() | ||
setenv(Wiretapp.recordEnabled, "false", 1) | ||
let packageRootPath = URL(fileURLWithPath: #file) | ||
.pathComponents | ||
.prefix(while: { $0 != "Tests" }) | ||
.joined(separator: "/") | ||
.dropFirst() | ||
setenv(Wiretapp.testCaseName, | ||
packageRootPath + "/Example/demo.wiretapp/MockResponses/WireTappUITests/ExampleTestDynamicFolder", | ||
1 | ||
) | ||
let expectation = XCTestExpectation(description: "data will load from our test folder instead of web") | ||
|
||
URLSession.shared.dataTask( | ||
with: URL(string: "https://jsonplaceholder.typicode.com/users")! | ||
) { data, response, error in | ||
if | ||
let data = data, | ||
let decoded = try? JSONDecoder().decode([User].self, from: data), | ||
decoded.first?.name == "FirstName LastName" | ||
{ | ||
expectation.fulfill() | ||
} | ||
} | ||
.resume() | ||
wait(for: [expectation], timeout: 10.0) | ||
} | ||
|
||
func test_loadingMultipleMockResponsesForSameURI() { | ||
Wiretapp.registerSharedSession() | ||
setenv(Wiretapp.recordEnabled, "false", 1) | ||
let packageRootPath = URL(fileURLWithPath: #file) | ||
.pathComponents | ||
.prefix(while: { $0 != "Tests" }) | ||
.joined(separator: "/") | ||
.dropFirst() | ||
setenv(Wiretapp.testCaseName, | ||
packageRootPath + "/Example/demo.wiretapp/MockResponses/WireTappUITests/ExampleTestMultipleResponses", | ||
1 | ||
) | ||
let expectation = XCTestExpectation(description: "responses from same uri have loaded in sequence") | ||
|
||
func loadPosts(completion: @escaping ([Post]?) -> ()) { | ||
URLSession.shared.dataTask( | ||
with: URL(string: "https://jsonplaceholder.typicode.com/posts")! | ||
) { data, response, error in | ||
if | ||
let data = data, | ||
let decoded = try? JSONDecoder().decode([Post].self, from: data) | ||
{ | ||
completion(decoded) | ||
} | ||
completion(nil) | ||
} | ||
.resume() | ||
} | ||
|
||
loadPosts { posts in | ||
if posts?.count == 1 { | ||
loadPosts { posts in | ||
if posts?.count == 2 { | ||
loadPosts { posts in | ||
if posts?.count == 3 { | ||
expectation.fulfill() | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
wait(for: [expectation], timeout: 10.0) | ||
} | ||
} |