Skip to content

Commit

Permalink
Merge pull request #526 from nimblehq/feature/#508-swiftui-network-un…
Browse files Browse the repository at this point in the history
…it-test

[#508] Add Unit Test for Network Layer
  • Loading branch information
blyscuit authored Oct 9, 2023
2 parents 94b3a23 + fa5826a commit d10fa8b
Show file tree
Hide file tree
Showing 10 changed files with 146 additions and 6 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ gem "danger-swiftlint"
gem "danger-xcode_summary"
gem 'danger-swiftformat'
gem 'danger-xcov'
# Fix issue with Cocoapods 13.0 when activesupport is 7.1.0
gem 'activesupport', '~> 7.0.0', '>= 7.0.8'

plugins_path = File.join(File.dirname(__FILE__), 'fastlane', 'Pluginfile')
eval_gemfile(plugins_path) if File.exist?(plugins_path)
1 change: 1 addition & 0 deletions Tuist/Interfaces/SwiftUI/Project/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ def testing_pods
pod 'Nimble', '~> 11.0'
pod 'Sourcery'
pod 'SwiftFormat/CLI'
pod 'OHHTTPStubs/Swift', :configurations => ['Debug Staging', 'Debug Production']
end

target '{PROJECT_NAME}' do
Expand Down
3 changes: 0 additions & 3 deletions Tuist/Interfaces/UIKit/Project/.sourcery.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,3 @@ output:
args:
autoMockableTestableImports:
- {PROJECT_NAME}
autoMockableImports:
- RxSwift
- RxCocoa
7 changes: 4 additions & 3 deletions Tuist/Interfaces/UIKit/Project/Podfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
platform :ios, '11.0'
platform :ios, '13.0'
use_frameworks!
inhibit_all_warnings!

def testing_pods
pod 'Quick'
pod 'Nimble'
pod 'Quick', '~> 6.0'
pod 'Nimble', '~> 11.0'
pod 'Sourcery'
pod 'SwiftFormat/CLI'
pod 'OHHTTPStubs/Swift', :configurations => ['Debug Staging', 'Debug Production']
end

target '{PROJECT_NAME}' do
Expand Down
Empty file.
14 changes: 14 additions & 0 deletions {PROJECT_NAME}Tests/Sources/Dummy/Data/DummyNetworkModel.swift
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
}
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
}
}
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 {PROJECT_NAME}Tests/Sources/Utilities/NetworkStubber.swift
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
)
}
}
}

0 comments on commit d10fa8b

Please sign in to comment.