Skip to content

Commit

Permalink
refactor: refactor DaprTests
Browse files Browse the repository at this point in the history
  • Loading branch information
cnblogs-dudu committed Feb 6, 2023
1 parent b81706c commit d05109d
Showing 1 changed file with 39 additions and 7 deletions.
46 changes: 39 additions & 7 deletions test/Cnblogs.Architecture.IntegrationTests/DaprTests.cs
Original file line number Diff line number Diff line change
@@ -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<TestIntegrationEvent>();
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);
}
}

0 comments on commit d05109d

Please sign in to comment.