Skip to content

Commit

Permalink
NR/UnitTest: Workaround for xUnit replacing the SynchronizationContext
Browse files Browse the repository at this point in the history
  • Loading branch information
jbe2277 committed Aug 29, 2024
1 parent 8abaa9d commit 83f2455
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ namespace Test.NewsReader.Applications.Controllers;
public class AppControllerTest : TestBase
{
[Fact]
public void OpenSettingsView()
public void OpenSettingsView() => Run(context =>
{
StartApp();
var feed = Context.WaitForNotNull(Shell.View.GetCurrentViewOrNull<MockFeedView, FeedViewModel>);
var feed = context.WaitForNotNull(Shell.View.GetCurrentViewOrNull<MockFeedView, FeedViewModel>);
var feed1 = FeedsController.FeedManager.Feeds.Single();
Assert.Equal(feed1, feed.ViewModel.Feed);
Assert.Equal(feed1, Shell.ViewModel.SelectedFeed);
Expand All @@ -20,5 +20,5 @@ public void OpenSettingsView()
var settings = Shell.View.GetCurrentView<MockSettingsView, SettingsViewModel>();
Assert.NotNull(settings.View);
Assert.Null(Shell.ViewModel.SelectedFeed);
}
});
}
10 changes: 6 additions & 4 deletions src/NewsReader/NewsReader.Applications.Test/TestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,25 @@ public abstract class TestBase : IDisposable
{
protected TestBase()
{
Context = UnitTestSynchronizationContext.Create();
var builder = new ContainerBuilder();
builder.RegisterModule(new ApplicationsModule());
builder.RegisterModule(new MockPresentationModule());
Container = builder.Build();
}

public UnitTestSynchronizationContext Context { get; }

public Autofac.IContainer Container { get; }

internal AppController AppController { get; private set; } = null!;
internal FeedsController FeedsController { get; private set; } = null!;
internal SettingsController SettingsController { get; private set; } = null!;
public ViewPair<MockShellView, ShellViewModel> Shell { get; private set; } = null!;

public void Run(Action<UnitTestSynchronizationContext> action)
{
using var context = UnitTestSynchronizationContext.Create();
action(context);
}

public T Resolve<T>() where T : notnull => Container.Resolve<T>();

public void StartApp()
Expand All @@ -47,7 +50,6 @@ public void StartApp()
public void Dispose()
{
OnDispose();
Context.Dispose();
}

protected virtual void OnDispose() { }
Expand Down

0 comments on commit 83f2455

Please sign in to comment.