El problema

Queremos hacer un test de las sucripción y ver si nuestro código funciona.

Solución

Entra en este ejemplo y a continuación os iré explicando los puntos importantes: https://github.com/jmfloreszazo/AzureEventHubDaprWithIntegrationTest

using System;
using System.Net;
using System.Threading.Tasks;
using CloudNative.CloudEvents;
using CloudNative.CloudEvents.Http;
using CloudNative.CloudEvents.NewtonsoftJson;
using FluentAssertions;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.VisualStudio.TestPlatform.TestHost;
using Newtonsoft.Json;
using PubSubDaprSample.Contracts;
using Xunit;

namespace TestProject;

public class DemoOptions
{
    public string OptionsConfigProperty { get; set; }
}

public class UnitTest1 : IClassFixture<WebApplicationFactory<Program>>
{
    private readonly WebApplicationFactory<Program> _tuneFactory;

    public UnitTest1(WebApplicationFactory<Program> factory)
    {
        _tuneFactory = factory.WithWebHostBuilder(builder =>
        {
            builder.ConfigureTestServices(services =>
            {
                services.Configure<DemoOptions>(opts => { opts.
      OptionsConfigProperty = "OverriddenValue"; });
            });
        });
    }

    [Fact]
    public async Task Test1()
    {
        //Arrange
        var someObject = new SomeObject {PropA = "Test1", PropB = "Test2"};
        var url = "test/subscriptor";
        var evt = new CloudEvent
        {
            Type = "test",
            Source = new Uri("http://localhost"),
            Time = DateTimeOffset.Now,
            DataContentType = "application/json",
            Id = Guid.NewGuid().ToString(),
            Data = JsonConvert.SerializeObject(someObject)
        };
        var content = evt.ToHttpContent(ContentMode.Structured, new JsonEventFormatter());

        //Act
        var client = _tuneFactory.CreateClient();
        var response = await client.PostAsync(url, content);

        //Assert
        response.StatusCode.Should().Be(HttpStatusCode.OK);
    }
}

La parte más importante es la generación del CloudEvent, para que se puedea mapear el siguiente Controller:

        [HttpPost("subscriptor")]
        [Topic("sub-microservice", "mydaprdemoeventhub")]
        public async Task<IActionResult> Subscriptor(JsonElement obj)
        {
            var eventPayload = JsonConvert.DeserializeObject<SomeObject>(obj.ToString());
            return new OkResult();
        }

Gracias al ejemplo podrás realizar una emulación de un envio de un CloudEvent… ya solo te queda ampliar los test, pero lo más complejo ya lo tienes funcionando.

Hasta el próximo artículo de Dapr, aun me quedan más wink