修正单元测试错误
大石头 authored at 2021-04-14 23:27:59
1.97 KiB
Stardust
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
using NewLife;
using NewLife.Data;
using NewLife.Remoting;
using Xunit;

namespace Stardust.Server.Controllers.Tests
{
    public class ApiTests
    {
        private readonly TestServer _server;

        public ApiTests()
        {
            _server = new TestServer(WebHost.CreateDefaultBuilder()
                .UseStartup<Startup>());
        }

        [Theory(DisplayName = "基础测试")]
        [InlineData("abcdef")]
        [InlineData(null)]
        public async Task BasicTest(String state)
        {
            var client = _server.CreateClient();

            var response = await client.GetAsync<HttpResponseMessage>("api", new { state });
            response.EnsureSuccessStatusCode();
            Assert.Equal("application/json", response.Content.Headers.ContentType + "");

            var rs = await client.GetAsync<IDictionary<String, Object>>("api", new { state });
            Assert.Null(rs["state"]);
        }

        [Theory(DisplayName = "信息测试")]
        [InlineData("abcdef")]
        [InlineData(null)]
        public async Task InfoTest(String state)
        {
            var client = _server.CreateClient();

            var rs = await client.GetAsync<IDictionary<String, Object>>("api/info", new { state });
            if (state.IsNullOrEmpty())
                Assert.Null(rs["state"]);
            else
                Assert.NotNull(rs["state"]);
            Assert.Equal(state + "", rs["state"] + "");
        }

        [Theory(DisplayName = "信息测试2")]
        [InlineData("abcdef")]
        [InlineData(null)]
        public async Task Info2Test(String state)
        {
            var client = _server.CreateClient();

            var rs = await client.PostAsync<Packet>("api/info2", state.ToHex());
            Assert.NotNull(rs);
        }
    }
}