Fix up the failing unit tests

This commit is contained in:
Antony Male 2019-09-22 15:07:38 +01:00
parent 3c7639bdc8
commit b3064171c1
7 changed files with 84 additions and 49 deletions

View File

@ -255,14 +255,16 @@ namespace StyletUnitTests
[Test]
public void DoesNotRetainTarget()
{
var view = new DependencyObject();
var weakView = new WeakReference(view);
View.SetActionTarget(view, this.target);
var cmd = new CommandAction(view, null, "DoSomethingWithGuard", ActionUnavailableBehaviour.Throw, ActionUnavailableBehaviour.Throw);
WeakReference Test()
{
var view = new DependencyObject();
View.SetActionTarget(view, this.target);
var cmd = new CommandAction(view, null, "DoSomethingWithGuard", ActionUnavailableBehaviour.Throw, ActionUnavailableBehaviour.Throw);
return new WeakReference(view);
}
var weakView = Test();
view = null;
cmd = null;
GC.Collect();
Assert.False(weakView.IsAlive);

View File

@ -235,13 +235,16 @@ namespace StyletUnitTests
[Test]
public void DoesNotRetainTarget()
{
var view = new DependencyObject();
var weakView = new WeakReference(view);
View.SetActionTarget(view, this.target);
var cmd = new EventAction(view, null, this.eventInfo.EventHandlerType, "DoSomething", ActionUnavailableBehaviour.Throw, ActionUnavailableBehaviour.Throw);
WeakReference Test()
{
var view = new DependencyObject();
View.SetActionTarget(view, this.target);
var cmd = new EventAction(view, null, this.eventInfo.EventHandlerType, "DoSomething", ActionUnavailableBehaviour.Throw, ActionUnavailableBehaviour.Throw);
return new WeakReference(view);
}
var weakView = Test();
cmd = null;
view = null;
GC.Collect();
Assert.IsFalse(weakView.IsAlive);

View File

@ -99,16 +99,19 @@ namespace StyletUnitTests
[Test]
public void TargetReferenceIsWeak()
{
var target = new C3();
var weaktarget = new WeakReference(target);
this.ea.Subscribe(target);
WeakReference Test()
{
var target = new C3();
this.ea.Subscribe(target);
return new WeakReference(target);
}
var weakTarget = Test();
// Ugly, but it's the only way to test a WeakReference...
target = null;
GC.Collect();
Assert.DoesNotThrow(() => this.ea.Publish(new M1()));
Assert.IsNull(weaktarget.Target);
Assert.IsNull(weakTarget.Target);
}
[Test]

View File

@ -79,16 +79,21 @@ namespace StyletUnitTests
[Test]
public void StrongBindingDoesNotRetainNotifier()
{
var binding = new BindingClass();
var notifying = new NotifyingClass();
// Means of determining whether the class has been disposed
var weakNotifying = new WeakReference<NotifyingClass>(notifying);
// Retain the IPropertyChangedBinding, in case that causes NotifyingClass to be retained
var binder = binding.BindStrong(notifying);
WeakReference Test(out IEventBinding binder)
{
var binding = new BindingClass();
var notifying = new NotifyingClass();
// Retain the IPropertyChangedBinding, in case that causes NotifyingClass to be retained
binder = binding.BindStrong(notifying);
return new WeakReference(notifying);
}
var weakNotifying = Test(out var retained);
notifying = null;
GC.Collect();
Assert.IsFalse(weakNotifying.TryGetTarget(out notifying));
Assert.IsFalse(weakNotifying.IsAlive);
GC.KeepAlive(retained);
}
[Test]

View File

@ -85,11 +85,15 @@ namespace StyletUnitTests
[Test]
public void ActivateWithDoesNotRetainChild()
{
var child = new Screen();
child.ActivateWith(this.parent);
WeakReference Test()
{
var child = new Screen();
child.ActivateWith(this.parent);
return new WeakReference(child);
}
var weakChild = Test();
var weakChild = new WeakReference(child);
child = null;
GC.Collect();
((IScreenState)this.parent).Activate();
@ -117,11 +121,15 @@ namespace StyletUnitTests
[Test]
public void DeactivateDoesNotRetainChild()
{
var child = new Screen();
child.DeactivateWith(this.parent);
WeakReference Test()
{
var child = new Screen();
child.DeactivateWith(this.parent);
return new WeakReference(child);
}
var weakChild = Test();
var weakChild = new WeakReference(child);
child = null;
GC.Collect();
((IScreenState)this.parent).Deactivate();
@ -149,11 +157,15 @@ namespace StyletUnitTests
[Test]
public void CloseWithDoesNotRetain()
{
var child = new Screen();
child.CloseWith(this.parent);
WeakReference Test()
{
var child = new Screen();
child.CloseWith(this.parent);
return new WeakReference(child);
}
var weakChild = Test();
var weakChild = new WeakReference(child);
child = null;
GC.Collect();
((IScreenState)this.parent).Close();
@ -171,11 +183,15 @@ namespace StyletUnitTests
[Test]
public void ConductWithDoesNotRetain()
{
var child = new Screen();
child.ConductWith(this.parent);
WeakReference Test()
{
var child = new Screen();
child.ConductWith(this.parent);
return new WeakReference(child);
}
var weakChild = Test();
var weakChild = new WeakReference(child);
child = null;
GC.Collect();
Assert.Null(weakChild.Target);

View File

@ -198,13 +198,19 @@ namespace StyletUnitTests
[Test]
public void DisposedSingletonRegistrationDoesNotRetainInstance()
{
var builder = new StyletIoCBuilder();
builder.Bind<C1>().ToSelf().InSingletonScope();
var ioc = builder.BuildContainer();
WeakReference Test(out IContainer ioc)
{
var builder = new StyletIoCBuilder();
builder.Bind<C1>().ToSelf().InSingletonScope();
ioc = builder.BuildContainer();
return new WeakReference(ioc.Get<C1>());
}
var weakRef = Test(out var container);
container.Dispose();
var weakRef = new WeakReference(ioc.Get<C1>());
ioc.Dispose();
GC.Collect();
Assert.IsFalse(weakRef.IsAlive);
}

View File

@ -1,7 +1,7 @@
<Project Sdk="MSBuild.Sdk.Extras">
<PropertyGroup>
<TargetFrameworks>net45;netcoreapp3.0</TargetFrameworks>
<TargetFrameworks>netcoreapp3.0;net45</TargetFrameworks>
<UseWpf>true</UseWpf>
<IsPackable>false</IsPackable>