LingAdmin/Backend/LingAdmin.API/TESTING.md

4.8 KiB

测试指南

API 已成功启动! 🎉

应用运行在:

测试方式

1. 使用 Swagger UI (推荐,最简单)

在浏览器中打开: http://localhost:5000/swagger

Swagger UI 提供了可视化的 API 测试界面,您可以:

  • 查看所有 API 端点
  • 直接在浏览器中测试每个 API
  • 查看请求和响应格式

2. 使用 PowerShell / curl 测试

获取用户列表

Invoke-RestMethod -Uri "http://localhost:5000/api/users" -Method Get | ConvertTo-Json -Depth 5

获取单个用户

Invoke-RestMethod -Uri "http://localhost:5000/api/users/1" -Method Get | ConvertTo-Json -Depth 5

获取请购单列表

Invoke-RestMethod -Uri "http://localhost:5000/api/requisitions" -Method Get | ConvertTo-Json -Depth 5

获取单个请购单

Invoke-RestMethod -Uri "http://localhost:5000/api/requisitions/101" -Method Get | ConvertTo-Json -Depth 5

获取菜单列表

Invoke-RestMethod -Uri "http://localhost:5000/api/menus" -Method Get | ConvertTo-Json -Depth 5

获取国际化语言清单

Invoke-RestMethod -Uri "http://localhost:5000/api/i18n/manifest" -Method Get | ConvertTo-Json -Depth 5

获取英文翻译

Invoke-RestMethod -Uri "http://localhost:5000/api/i18n/lang/en-US" -Method Get | ConvertTo-Json -Depth 5

获取中文翻译

Invoke-RestMethod -Uri "http://localhost:5000/api/i18n/lang/zh-CN" -Method Get | ConvertTo-Json -Depth 5

创建新请购单

$body = @{
    requester = "测试用户"
    department = "IT部门"
    date = "2026-01-28"
    status = "Draft"
    description = "测试请购单"
    items = @(
        @{
            itemName = "测试物品1"
            quantity = 2
            price = 100.50
        },
        @{
            itemName = "测试物品2"
            quantity = 5
            price = 50.00
        }
    )
} | ConvertTo-Json -Depth 5

Invoke-RestMethod -Uri "http://localhost:5000/api/requisitions" `
    -Method Post `
    -Body $body `
    -ContentType "application/json" | ConvertTo-Json -Depth 5

创建新用户

$newUser = @{
    name = "测试用户"
    email = "test@example.com"
    role = "User"
    status = "Active"
} | ConvertTo-Json

Invoke-RestMethod -Uri "http://localhost:5000/api/users" `
    -Method Post `
    -Body $newUser `
    -ContentType "application/json" | ConvertTo-Json -Depth 5

3. 通过 Dapr Sidecar 访问 (使用 Dapr 服务调用)

# 通过 Dapr 调用 API
Invoke-RestMethod -Uri "http://localhost:3500/v1.0/invoke/lingadmin-api/method/api/users" -Method Get | ConvertTo-Json -Depth 5

4. 测试 Dapr 状态管理

# 保存状态
$state = @{
    key = "test-key"
    value = "test-value"
}
Invoke-RestMethod -Uri "http://localhost:3500/v1.0/state/statestore" `
    -Method Post `
    -Body ($state | ConvertTo-Json) `
    -ContentType "application/json"

# 读取状态
Invoke-RestMethod -Uri "http://localhost:3500/v1.0/state/statestore/test-key" -Method Get

5. 查看 Dapr Dashboard

在新的 PowerShell 窗口运行:

dapr dashboard

然后访问: http://localhost:8080

Dashboard 可以查看:

  • 应用状态
  • 组件配置
  • 日志和追踪

6. 使用 Postman 或其他 API 工具

导入以下 API 端点到 Postman:

基础 URL: http://localhost:5000

端点列表:

  • GET /api/users - 获取所有用户
  • GET /api/users/{id} - 获取单个用户
  • POST /api/users - 创建用户
  • PUT /api/users/{id} - 更新用户
  • DELETE /api/users/{id} - 删除用户
  • GET /api/requisitions - 获取所有请购单
  • GET /api/requisitions/{id} - 获取单个请购单
  • POST /api/requisitions - 创建请购单
  • PUT /api/requisitions/{id} - 更新请购单
  • DELETE /api/requisitions/{id} - 删除请购单
  • GET /api/i18n/manifest - 获取语言清单
  • GET /api/i18n/lang/{languageCode} - 获取翻译
  • GET /api/menus - 获取菜单

响应格式

所有 API 返回统一格式:

{
  "code": 200,
  "message": "success",
  "data": { /* 实际数据 */ }
}

重启应用

# 停止应用 (Ctrl+C)

# 重新启动
cd D:\Blank\Repos\LingAdmin\Backend\LingAdmin.API
dapr run --app-id lingadmin-api --app-port 5000 --dapr-http-port 3500 --resources-path ".\dapr\components" -- dotnet run

故障排查

查看日志

应用日志会显示在运行 dapr run 的终端中

检查数据库

使用 SQL Server Management Studio 或 Azure Data Studio 连接到:

  • Server: 127.0.0.1,19433
  • Database: LingAdminDB
  • User: sa
  • Password: Blank@0925

检查 Redis (Dapr 状态存储)

docker ps | findstr redis