C# Akka.Net 单元测试哪个内置断言等待发布的消息是正确的?
C# Akka.Net Unit Tests which built-In Assertion is the correct to wait for a published message?
我需要一些支持才能理解如何成功等待从 actor Receive 块中发送的预期响应消息。
以下示例与我目前在网上找到的不同,因为我使用了 Akka.Cluster / Akka.Cluster.Tools
发布消息而不是直接将消息发送回“Sender.Tell()
”给发件人。
问题:如何在我的单元测试中获取、订阅此 已发布 SomeResponse
消息?
public class SomeRequest { }
public class SomeResponse { }
public class MyDbTestActor : ReceiveActor
{
private readonly IActorRef _mediator;
public MyDbTestActor()
{
// ...
_mediator = DistributedPubSub.Get(Context.System).Mediator;
// ...
Receive<SomeRequest>(o =>
{
// ...
var response = new SomeResponse();
_mediator.Tell(new Publish(Global.MYTopic, response)); // <-- ExpectMsg timeout ...
//Sender.Tell(response); // if uncommented ExpectMsg is succeed.
});
}
}
class Global
{
public static string MYTopic =nameof(MYTopic);
}
public class SomeActorTests : TestKit
{
[Fact]
public void SomeTest()
{
var actor = Sys.ActorOf(Props.Create<MyDbTestActor>());
actor.Tell(new SomeRequest());
ExpectMsg<SomeResponse>();
}
}
我想让 TestActor
从 DistributedPubSub
调解员那里订阅该主题:
public class SomeActorTests: TestKit
{
private IActorRef _mediator;
private string path;
// pass in an Akka.Cluster config that performs a self-join, etc
public SomeActorTests(Config config, ITestOutputHelper output) : base(config, output:output)
{
_mediator = DistributedPubSub.Get(Sys).Mediator;
}
[Fact]
public void SomeTest()
{
// arrange
var actor = Sys.ActorOf(Props.Create<MyDbTestActor>());
_mediator.Tell(new Subscribe(Global.MYTopic, TestActor));
ExpectMsg<SubscribeAck>(); // subscription confirmed
// act
actor.Tell(new SomeRequest());
// assert
ExpectMsg<SomeResponse>();
}
}
这将使用 Mediator
将您的演员和 TestActor
连接在一起来关闭电路。
我需要一些支持才能理解如何成功等待从 actor Receive 块中发送的预期响应消息。
以下示例与我目前在网上找到的不同,因为我使用了 Akka.Cluster / Akka.Cluster.Tools
发布消息而不是直接将消息发送回“Sender.Tell()
”给发件人。
问题:如何在我的单元测试中获取、订阅此 已发布 SomeResponse
消息?
public class SomeRequest { }
public class SomeResponse { }
public class MyDbTestActor : ReceiveActor
{
private readonly IActorRef _mediator;
public MyDbTestActor()
{
// ...
_mediator = DistributedPubSub.Get(Context.System).Mediator;
// ...
Receive<SomeRequest>(o =>
{
// ...
var response = new SomeResponse();
_mediator.Tell(new Publish(Global.MYTopic, response)); // <-- ExpectMsg timeout ...
//Sender.Tell(response); // if uncommented ExpectMsg is succeed.
});
}
}
class Global
{
public static string MYTopic =nameof(MYTopic);
}
public class SomeActorTests : TestKit
{
[Fact]
public void SomeTest()
{
var actor = Sys.ActorOf(Props.Create<MyDbTestActor>());
actor.Tell(new SomeRequest());
ExpectMsg<SomeResponse>();
}
}
我想让 TestActor
从 DistributedPubSub
调解员那里订阅该主题:
public class SomeActorTests: TestKit
{
private IActorRef _mediator;
private string path;
// pass in an Akka.Cluster config that performs a self-join, etc
public SomeActorTests(Config config, ITestOutputHelper output) : base(config, output:output)
{
_mediator = DistributedPubSub.Get(Sys).Mediator;
}
[Fact]
public void SomeTest()
{
// arrange
var actor = Sys.ActorOf(Props.Create<MyDbTestActor>());
_mediator.Tell(new Subscribe(Global.MYTopic, TestActor));
ExpectMsg<SubscribeAck>(); // subscription confirmed
// act
actor.Tell(new SomeRequest());
// assert
ExpectMsg<SomeResponse>();
}
}
这将使用 Mediator
将您的演员和 TestActor
连接在一起来关闭电路。