SpeziMockWebService

Build and Test codecov DOI

模拟基于 Spezi 的应用程序与 Web 服务的交互。

概述

Spezi Mock Web Service Swift 包提供了一个 Spezi 模块 (MockWebService),用于模拟与 Web 服务的交互,并在用户界面中显示请求,可用于演示和调试目的 (RequestList)。

Screenshot showing an empty list with a placeholder stating: 'The Mock Web Service will display all requests that would be triggered by the application.' Screenshot showing an empty list with a placeholder stating: 'The Mock Web Service will display all requests that would be triggered by the application.' Screenshot showing two requests in the mock request list: One is a deletion request, and one is an addition. Screenshot showing two requests in the mock request list: One is a deletion request, and one is an addition. Detail view of a mock request, showing that it is an addition with a short JSON body. Detail view of a mock request, showing that it is an addition with a short JSON body.
RequestList 的登陆页面 RequestList 提供了所有已发送请求的概览。 单个请求的详细视图。

设置

1. 添加 Spezi Mock Web Service 作为依赖项

您需要在 Xcode 中的应用程序Swift 包 中添加 Spezi Mock Web Service Swift 包。

重要提示

如果您的应用程序尚未配置为使用 Spezi,请按照 Spezi 设置文章 设置核心 Spezi 基础设施。

2. 注册模块

需要在基于 Spezi 的应用程序中使用 configurationSpeziAppDelegate 中注册 MockWebService 模块

class ExampleAppDelegate: SpeziAppDelegate {
    override var configuration: Configuration {
        Configuration {
            MockWebService()
            // ...
        }
    }
}

注意

您可以在 Spezi 文档中了解更多关于 Module 的信息

示例

以下示例演示了 Swift 包在 SwiftUI View 中的用法,使用 @Environment 属性包装器访问 MockWebService

import SpeziMockWebService
import SwiftUI


struct ExampleView: View {
    @Environment(MockWebService.self) var webService: MockWebService
    
    
    var body: some View {
        NavigationStack {
            RequestList()
            // ...
        }
    }
    
    
    private func sendMockUploadRequests() async throws {
        try await webService.upload(path: "Test", body: #"{"test": "test"}"#)
        try await webService.remove(path: "TestRemoval")
    }
}

有关更多信息,请参阅 API 文档

贡献

欢迎对本项目做出贡献。请务必先阅读 贡献指南贡献者公约行为准则

许可证

本项目采用 MIT 许可证。有关更多信息,请参阅 许可证

Spezi Footer Spezi Footer