✅ Add unit testing
This commit is contained in:
19
.github/workflows/main.yml
vendored
Normal file
19
.github/workflows/main.yml
vendored
Normal file
@@ -0,0 +1,19 @@
|
||||
name: Hermes CI
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ main, develop ]
|
||||
pull_request:
|
||||
branches: [ main, develop ]
|
||||
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- name: Set up Swift
|
||||
uses: fwal/setup-swift@v1
|
||||
- name: Build
|
||||
run: swift build
|
||||
- name: Run tests
|
||||
run: swift test
|
||||
@@ -1,12 +1,61 @@
|
||||
import XCTest
|
||||
@testable import Hermes
|
||||
|
||||
final class HermesTests: XCTestCase {
|
||||
func testExample() throws {
|
||||
// XCTest Documentation
|
||||
// https://developer.apple.com/documentation/xctest
|
||||
struct SomeCodableStruct: Codable, Equatable {
|
||||
let url: String
|
||||
var data: String? = nil
|
||||
var json: SomeCodableJsonStruct? = nil
|
||||
}
|
||||
|
||||
// Defining Test Cases and Test Methods
|
||||
// https://developer.apple.com/documentation/xctest/defining_test_cases_and_test_methods
|
||||
struct SomeCodableJsonStruct: Codable, Equatable {
|
||||
var test: String
|
||||
}
|
||||
|
||||
final class HermesTests: XCTestCase {
|
||||
func testResourceInitialization() {
|
||||
let url = URL(string: "https://httpbin.org/get")!
|
||||
let resource = Resource<SomeCodableStruct>(url: url, method: .get([]), modelType: SomeCodableStruct.self)
|
||||
|
||||
XCTAssertEqual(resource.url, url)
|
||||
XCTAssertEqual(resource.method.name, "GET")
|
||||
XCTAssert(resource.modelType == SomeCodableStruct.self)
|
||||
}
|
||||
|
||||
func testGetRequest() async throws {
|
||||
let url = URL(string: "https://httpbin.org/get")!
|
||||
let resource = Resource(url: url, method: .get([]), modelType: SomeCodableStruct.self)
|
||||
|
||||
let response = try await Hermes().load(resource)
|
||||
|
||||
XCTAssertNotNil(response)
|
||||
XCTAssertEqual(response.url, "https://httpbin.org/get")
|
||||
XCTAssertNil(response.data)
|
||||
XCTAssertNil(response.json)
|
||||
}
|
||||
|
||||
func testPostRequest() async throws {
|
||||
let postDatas = ["test": "test"]
|
||||
|
||||
let url = URL(string: "https://httpbin.org/post")!
|
||||
let resource = try Resource(url: url, method: .post(JSONEncoder().encode(postDatas)), modelType: SomeCodableStruct.self)
|
||||
|
||||
let response = try await Hermes().load(resource)
|
||||
|
||||
XCTAssertNotNil(response)
|
||||
XCTAssertEqual(response.url, "https://httpbin.org/post")
|
||||
XCTAssertEqual(response.data, "{\"test\":\"test\"}")
|
||||
XCTAssert(response.json == SomeCodableJsonStruct(test: "test"))
|
||||
}
|
||||
|
||||
func testDeleteRequest() async throws {
|
||||
let url = URL(string: "https://httpbin.org/delete")!
|
||||
let resource = Resource(url: url, method: .delete, modelType: SomeCodableStruct.self)
|
||||
|
||||
let response = try await Hermes().load(resource)
|
||||
|
||||
XCTAssertNotNil(response)
|
||||
XCTAssertEqual(response.url, "https://httpbin.org/delete")
|
||||
XCTAssertEqual(response.data, "")
|
||||
XCTAssertNil(response.json)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user