Browse Source

Switch to NSubtitute for tests

pull/179/head
Ionite 1 year ago
parent
commit
e3928807a1
No known key found for this signature in database
  1. 38
      StabilityMatrix.Tests/Avalonia/LoadableViewModelBaseTests.cs
  2. 7
      StabilityMatrix.Tests/StabilityMatrix.Tests.csproj

38
StabilityMatrix.Tests/Avalonia/LoadableViewModelBaseTests.cs

@ -2,9 +2,8 @@
using System.Text.Json.Serialization;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Moq;
using NSubstitute;
using StabilityMatrix.Avalonia.Models;
using StabilityMatrix.Avalonia.ViewModels;
using StabilityMatrix.Avalonia.ViewModels.Base;
#pragma warning disable CS0657 // Not a valid attribute location for this declaration
@ -90,11 +89,7 @@ public class LoadableViewModelBaseTests
public void TestSaveStateToJsonObject_Observable()
{
// Mvvm ObservableProperty should be serialized.
var vm = new TestLoadableViewModelObservable
{
Title = "abc",
Id = 123,
};
var vm = new TestLoadableViewModelObservable { Title = "abc", Id = 123, };
var state = vm.SaveStateToJsonObject();
// Title should be ignored since it has [JsonIgnore]
@ -112,12 +107,9 @@ public class LoadableViewModelBaseTests
// SaveStateToJsonObject method.
// Make a mock IJsonLoadableState
var mockState = new Mock<IJsonLoadableState>();
var mockState = Substitute.For<IJsonLoadableState>();
var vm = new TestLoadableViewModelNestedInterface
{
NestedState = mockState.Object
};
var vm = new TestLoadableViewModelNestedInterface { NestedState = mockState };
// Serialize
var state = vm.SaveStateToJsonObject();
@ -126,7 +118,7 @@ public class LoadableViewModelBaseTests
Assert.AreEqual(1, state.Count);
// Check that SaveStateToJsonObject was called
mockState.Verify(x => x.SaveStateToJsonObject(), Times.Once);
mockState.Received().SaveStateToJsonObject();
}
[TestMethod]
@ -164,10 +156,7 @@ public class LoadableViewModelBaseTests
Ignored = 456,
};
var vm = new TestLoadableViewModelNested
{
NestedState = nested
};
var vm = new TestLoadableViewModelNested { NestedState = nested };
var state = vm.SaveStateToJsonObject();
@ -195,10 +184,7 @@ public class LoadableViewModelBaseTests
Ignored = 456,
};
var vm = new TestLoadableViewModelNestedInterface
{
NestedState = nested
};
var vm = new TestLoadableViewModelNestedInterface { NestedState = nested };
var state = vm.SaveStateToJsonObject();
@ -241,16 +227,16 @@ public class LoadableViewModelBaseTests
{
var vm = new TestLoadableViewModelReadOnlyLoadable
{
ReadOnlyLoadable =
{
Included = "abc-123"
}
ReadOnlyLoadable = { Included = "abc-123" }
};
var state = vm.SaveStateToJsonObject();
// Check readonly loadable property was serialized
Assert.AreEqual(1, state.Count);
Assert.AreEqual("abc-123", state["ReadOnlyLoadable"].Deserialize<TestLoadableViewModel>()!.Included);
Assert.AreEqual(
"abc-123",
state["ReadOnlyLoadable"].Deserialize<TestLoadableViewModel>()!.Included
);
}
}

7
StabilityMatrix.Tests/StabilityMatrix.Tests.csproj

@ -15,13 +15,13 @@
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="7.0.8" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageReference Include="Moq" Version="4.18.4" />
<PackageReference Include="MSTest.TestAdapter" Version="3.1.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.0.4" />
<PackageReference Include="coverlet.collector" Version="6.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="NSubstitute" Version="5.1.0" />
<PackageReference Include="Polly" Version="7.2.4" />
<PackageReference Include="Polly.Contrib.WaitAndRetry" Version="1.1.1" />
</ItemGroup>
@ -32,7 +32,10 @@
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="..\StabilityMatrix.Avalonia\Assets\ImagePrompt.tmLanguage.json" />
<EmbeddedResource Include="..\StabilityMatrix.Avalonia\Assets\ThemeMatrixDark.json" />
</ItemGroup>
</Project>

Loading…
Cancel
Save