From d05109d87c6190281ce6682ff1ecbcbd4237470f Mon Sep 17 00:00:00 2001 From: dudu Date: Mon, 6 Feb 2023 11:47:15 +0800 Subject: [PATCH] refactor: refactor DaprTests --- .../DaprTests.cs | 46 ++++++++++++++++--- 1 file changed, 39 insertions(+), 7 deletions(-) diff --git a/test/Cnblogs.Architecture.IntegrationTests/DaprTests.cs b/test/Cnblogs.Architecture.IntegrationTests/DaprTests.cs index 37b037a..714af76 100644 --- a/test/Cnblogs.Architecture.IntegrationTests/DaprTests.cs +++ b/test/Cnblogs.Architecture.IntegrationTests/DaprTests.cs @@ -1,27 +1,59 @@ -using System.Net; +using System.Diagnostics; +using System.Net; +using Cnblogs.Architecture.TestIntegrationEvents; using FluentAssertions; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Routing; +using Microsoft.AspNetCore.TestHost; +using Microsoft.Extensions.DependencyInjection; namespace Cnblogs.Architecture.IntegrationTests; -[Collection(DddWebTestCollection.Name)] public class DaprTests { - private readonly HttpClient _httpClient; - - public DaprTests(DddWebTestFactory factory) + public DaprTests() { - _httpClient = factory.CreateClient(); } [Fact] public async Task Dapr_SubscribeEndpoint_OkAsync() { + // Arrange + var builder = WebApplication.CreateBuilder(); + builder.Services.AddDaprEventBus(nameof(DaprTests)); + builder.WebHost.UseTestServer(); + + var app = builder.Build(); + app.Subscribe(); + await app.StartAsync(); + var httpClient = app.GetTestClient(); + // Act - var response = await _httpClient.GetAsync("/dapr/subscribe"); + var response = await httpClient.GetAsync("/dapr/subscribe"); // Assert response.StatusCode.Should().Be(HttpStatusCode.OK); var responseText = await response.Content.ReadAsStringAsync(); responseText.Should().Contain("pubsub"); } + + [Fact] + public async Task Dapr_MapSubscribeHandler_OkAsync() + { + // Arrange + var builder = WebApplication.CreateBuilder(); + builder.Services.AddDaprClient(); + builder.WebHost.UseTestServer(); + + var app = builder.Build(); + app.MapSubscribeHandler(); + await app.StartAsync(); + var httpClient = app.GetTestClient(); + + // Act + var response = await httpClient.GetAsync("/dapr/subscribe"); + + // Assert + response.StatusCode.Should().Be(HttpStatusCode.OK); + } }