Skip to content

Commit

Permalink
Merge pull request #11 from cnblogs/refactor-testing
Browse files Browse the repository at this point in the history
refactor: refactor DaprTests
  • Loading branch information
ikesnowy authored Feb 6, 2023
2 parents b81706c + d05109d commit aeed017
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 aeed017

Please sign in to comment.