-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
141 additions
and
0 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
Empty file.
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// | ||
// DummyNetworkModel.swift | ||
// | ||
|
||
import Foundation | ||
|
||
struct DummyNetworkModel: Decodable { | ||
|
||
static let json = | ||
""" | ||
{"message": "Hello"} | ||
""" | ||
let message: String | ||
} |
29 changes: 29 additions & 0 deletions
29
{PROJECT_NAME}Tests/Sources/Dummy/DummyRequestConfiguration.swift
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// | ||
// DummyRequestConfiguration.swift | ||
// | ||
|
||
import Alamofire | ||
|
||
@testable import {PROJECT_NAME} | ||
|
||
struct DummyRequestConfiguration: RequestConfiguration { | ||
|
||
var baseURL: String { "https://example.com" } | ||
|
||
var endpoint: String { "" } | ||
|
||
var method: Alamofire.HTTPMethod { .get } | ||
|
||
var encoding: Alamofire.ParameterEncoding { URLEncoding.queryString } | ||
} | ||
|
||
extension DummyRequestConfiguration: RequestConfigurationStubable { | ||
|
||
var sampleData: Data { | ||
DummyNetworkModel.json.data(using: .utf8) ?? Data() | ||
} | ||
|
||
var path: String { | ||
(try? url.asURL().path).string | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
{PROJECT_NAME}Tests/Sources/Specs/Data/NetworkAPI/NetworkAPISpec.swift
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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// | ||
// NetworkAPISpec.swift | ||
// | ||
|
||
import Nimble | ||
import Quick | ||
|
||
@testable import {PROJECT_NAME} | ||
|
||
final class NetworkAPISpec: QuickSpec { | ||
|
||
override func spec() { | ||
|
||
describe("a NetworkAPI") { | ||
|
||
var networkAPI: NetworkAPI! | ||
var requestConfiguration: DummyRequestConfiguration! | ||
|
||
describe("its performRequest") { | ||
|
||
beforeEach { | ||
requestConfiguration = DummyRequestConfiguration() | ||
} | ||
|
||
afterEach { | ||
NetworkStubber.removeAllStubs() | ||
} | ||
|
||
context("when network returns value") { | ||
|
||
beforeEach { | ||
NetworkStubber.stub(requestConfiguration) | ||
networkAPI = NetworkAPI() | ||
} | ||
|
||
it("returns message as Hello") { | ||
let response = try await networkAPI.performRequest( | ||
requestConfiguration, | ||
for: DummyNetworkModel.self | ||
) | ||
expect(response.message) == "Hello" | ||
} | ||
} | ||
|
||
context("when network returns error") { | ||
|
||
beforeEach { | ||
NetworkStubber.stub(requestConfiguration, data: Data(), statusCode: 400) | ||
networkAPI = NetworkAPI() | ||
} | ||
|
||
it("throws error") { | ||
await expect { | ||
try await networkAPI.performRequest( | ||
requestConfiguration, | ||
for: DummyNetworkModel.self | ||
) | ||
}.to(throwError()) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
Empty file.
32 changes: 32 additions & 0 deletions
32
{PROJECT_NAME}Tests/Sources/Utilities/NetworkStubber.swift
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// | ||
// NetworkStubber.swift | ||
// | ||
|
||
import OHHTTPStubs | ||
|
||
protocol RequestConfigurationStubable { | ||
|
||
var sampleData: Data { get } | ||
var path: String { get } | ||
} | ||
|
||
enum NetworkStubber { | ||
|
||
static func removeAllStubs() { | ||
HTTPStubs.removeAllStubs() | ||
} | ||
|
||
static func stub( | ||
_ request: RequestConfigurationStubable, | ||
data: Data? = nil, | ||
statusCode: Int32 = 200 | ||
) { | ||
OHHTTPStubs.stub(condition: isPath(request.path)) { _ in | ||
HTTPStubsResponse( | ||
data: data ?? request.sampleData, | ||
statusCode: statusCode, | ||
headers: nil | ||
) | ||
} | ||
} | ||
} |