From e2b1d73e7293f68b9611232d5b1c3c08785ca86e Mon Sep 17 00:00:00 2001 From: Gregorius Soedharmo Date: Wed, 14 Sep 2022 21:04:46 +0700 Subject: [PATCH] [Akka.TestKit] TestKitBase should take arbitrary ActorSystem without throwing (#6092) (#6094) (cherry picked from commit 015de371f92717c2d4c4c4e6ae3252166335a7a2) --- src/core/Akka.TestKit.Tests/TestKitSpec.cs | 59 ++++++++++++++++++++++ src/core/Akka.TestKit/TestKitBase.cs | 4 ++ 2 files changed, 63 insertions(+) create mode 100644 src/core/Akka.TestKit.Tests/TestKitSpec.cs diff --git a/src/core/Akka.TestKit.Tests/TestKitSpec.cs b/src/core/Akka.TestKit.Tests/TestKitSpec.cs new file mode 100644 index 00000000000..5071af9d7e4 --- /dev/null +++ b/src/core/Akka.TestKit.Tests/TestKitSpec.cs @@ -0,0 +1,59 @@ +// ----------------------------------------------------------------------- +// +// Copyright (C) 2009-2022 Lightbend Inc. +// Copyright (C) 2013-2022 .NET Foundation +// +// ----------------------------------------------------------------------- + +using Akka.Actor; +using Akka.Actor.Dsl; +using Akka.Configuration; +using Xunit; +using Xunit.Abstractions; +using FluentAssertions; +using static FluentAssertions.FluentActions; + +namespace Akka.TestKit.Tests +{ + public class TestKitSpec + { + private readonly ITestOutputHelper _output; + + public TestKitSpec(ITestOutputHelper output) + { + _output = output; + } + + [Fact(DisplayName = "TestKit should accept arbitrary ActorSystem")] + public void TestKitBaseTest() + { + using (var sys = ActorSystem.Create(nameof(TestKitSpec))) + { + var testkit = new TestKit.Xunit2.TestKit(sys, _output); + var echoActor = testkit.Sys.ActorOf(c => c.ReceiveAny((m, ctx) => testkit.TestActor.Tell(m))); + Invoking(() => + { + echoActor.Tell("message"); + var message = testkit.ExpectMsg(); + message.Should().Be("message"); + }).Should().NotThrow(); + } + } + + [Fact(DisplayName = "TestKit should accept ActorSystem with TestKit.DefaultConfig")] + public void TestKitConfigTest() + { + using (var sys = ActorSystem.Create(nameof(TestKitSpec), TestKit.Xunit2.TestKit.DefaultConfig)) + { + var testkit = new TestKit.Xunit2.TestKit(sys, _output); + var echoActor = testkit.Sys.ActorOf(c => c.ReceiveAny((m, ctx) => testkit.TestActor.Tell(m))); + Invoking(() => + { + echoActor.Tell("message"); + var message = testkit.ExpectMsg(); + message.Should().Be("message"); + }).Should().NotThrow(); + } + } + } +} \ No newline at end of file diff --git a/src/core/Akka.TestKit/TestKitBase.cs b/src/core/Akka.TestKit/TestKitBase.cs index 0dd2786b423..49f1c86410d 100644 --- a/src/core/Akka.TestKit/TestKitBase.cs +++ b/src/core/Akka.TestKit/TestKitBase.cs @@ -147,6 +147,10 @@ protected virtual void InitializeTest(ActorSystem system, ActorSystemSetup confi } system = ActorSystem.Create(actorSystemName ?? "test", config.WithSetup(newBootstrap)); } + else + { + system.Settings.InjectTopLevelFallback(_defaultConfig); + } _testState.System = system;