我正在编写一个简单的Akka.NET流程。来源是IActorRef
。接收器是ISubscriber
。我正在使用TestKit将其作为单元测试来实现:
[Fact]
public void AkkaStreams_ActorSourcePublisherSink_Works()
{
using (var materializer = Sys.Materializer())
{
var probe = CreateTestProbe();
var source = Source.ActorRef<HandlerErrorEvent>(10, OverflowStrategy.DropNew);
var subscriber = new Mock<ISubscriber<HandlerErrorEvent>>();
var sink = Sink.FromSubscriber<HandlerErrorEvent>(subscriber.Object);
var graph = source.ToMaterialized(sink, Keep.Both);
var (actor, publisher) = graph.Run(materializer);
subscriber.Verify(s => s.OnSubscribe(It.IsAny<ISubscription>()));
var evnt = new HandlerErrorEvent("", HandlerResult.NotHandled);
actor.Tell(evnt, ActorRefs.Nobody);
base.AwaitCondition(() =>
{
try
{
subscriber.Verify(s => s.OnNext(It.IsAny<HandlerErrorEvent>()));
return true;
}
catch(MockException)
{
return false;
}
});
}
}
对Verify
方法的初始OnSubscribe
调用正常,但模拟订阅者永远不会收到对OnNext
的调用。
我做错了什么?
以netcoreapp2.0
身份运行。参考文献:
"Akka.TestKit.Xunit2" Version="1.3.2"
"Microsoft.NET.Test.Sdk" Version="15.5.0"
"Moq" Version="4.8.0-rc1"
"xunit" Version="2.3.1"
"xunit.runner.visualstudio" Version="2.3.1"
"dotnet-xunit" Version="2.3.1"
答案 0 :(得分:2)
您的ISubscriber<>
模拟不符合Reactive Streams specification。它指出,为了在订阅后获得任何消息,订户必须首先使用ISubscription.Request(long)方法传达需求。
一般情况下,如果您使用的是Akka.Streams测试工具包,则无需模拟订阅。只需下载Akka.Streams.TestKit即可获得Akka.Streams的扩展方法。这样,您只需在this.CreateManualSubscriberProbe<HandlerErrorEvent>();
课程中调用TestKit
即可构建虚假订阅者。它包含一个dozens of methods,可用于断言。
示例:
public class ExampleTest : TestKit
{
[Fact]
public void Select_should_map_output()
{
using (var materializer = Sys.Materializer())
{
// create test probe for subscriptions
var probe = this.CreateManualSubscriberProbe<int>();
// create flow materialized as publisher
var publisher = Source.From(new[] { 1, 2, 3 })
.Select(i => i + 1)
.RunWith(Sink.AsPublisher<int>(fanout: false), materializer);
// subscribe probe and receive subscription
publisher.Subscribe(probe);
var subscription = probe.ExpectSubscription();
// request number of elements to receive, here drain source utill the end
subscription.Request(4);
// validate assertions
probe.ExpectNext(2);
probe.ExpectNext(3);
probe.ExpectNext(4);
// since source had finite number of 3 elements, expect it to complete
probe.ExpectComplete();
}
}
}