Skip to content

Commit aeed017

Browse files
authored
Merge pull request #11 from cnblogs/refactor-testing
refactor: refactor DaprTests
2 parents b81706c + d05109d commit aeed017

File tree

1 file changed

+39
-7
lines changed

1 file changed

+39
-7
lines changed
Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,59 @@
1-
using System.Net;
1+
using System.Diagnostics;
2+
using System.Net;
3+
using Cnblogs.Architecture.TestIntegrationEvents;
24
using FluentAssertions;
5+
using Microsoft.AspNetCore.Builder;
6+
using Microsoft.AspNetCore.Routing;
7+
using Microsoft.AspNetCore.TestHost;
8+
using Microsoft.Extensions.DependencyInjection;
39

410
namespace Cnblogs.Architecture.IntegrationTests;
511

6-
[Collection(DddWebTestCollection.Name)]
712
public class DaprTests
813
{
9-
private readonly HttpClient _httpClient;
10-
11-
public DaprTests(DddWebTestFactory factory)
14+
public DaprTests()
1215
{
13-
_httpClient = factory.CreateClient();
1416
}
1517

1618
[Fact]
1719
public async Task Dapr_SubscribeEndpoint_OkAsync()
1820
{
21+
// Arrange
22+
var builder = WebApplication.CreateBuilder();
23+
builder.Services.AddDaprEventBus(nameof(DaprTests));
24+
builder.WebHost.UseTestServer();
25+
26+
var app = builder.Build();
27+
app.Subscribe<TestIntegrationEvent>();
28+
await app.StartAsync();
29+
var httpClient = app.GetTestClient();
30+
1931
// Act
20-
var response = await _httpClient.GetAsync("/dapr/subscribe");
32+
var response = await httpClient.GetAsync("/dapr/subscribe");
2133

2234
// Assert
2335
response.StatusCode.Should().Be(HttpStatusCode.OK);
2436
var responseText = await response.Content.ReadAsStringAsync();
2537
responseText.Should().Contain("pubsub");
2638
}
39+
40+
[Fact]
41+
public async Task Dapr_MapSubscribeHandler_OkAsync()
42+
{
43+
// Arrange
44+
var builder = WebApplication.CreateBuilder();
45+
builder.Services.AddDaprClient();
46+
builder.WebHost.UseTestServer();
47+
48+
var app = builder.Build();
49+
app.MapSubscribeHandler();
50+
await app.StartAsync();
51+
var httpClient = app.GetTestClient();
52+
53+
// Act
54+
var response = await httpClient.GetAsync("/dapr/subscribe");
55+
56+
// Assert
57+
response.StatusCode.Should().Be(HttpStatusCode.OK);
58+
}
2759
}

0 commit comments

Comments
 (0)