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. 110
      StabilityMatrix.Tests/Avalonia/LoadableViewModelBaseTests.cs
  2. 7
      StabilityMatrix.Tests/StabilityMatrix.Tests.csproj

110
StabilityMatrix.Tests/Avalonia/LoadableViewModelBaseTests.cs

@ -2,9 +2,8 @@
using System.Text.Json.Serialization; using System.Text.Json.Serialization;
using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input; using CommunityToolkit.Mvvm.Input;
using Moq; using NSubstitute;
using StabilityMatrix.Avalonia.Models; using StabilityMatrix.Avalonia.Models;
using StabilityMatrix.Avalonia.ViewModels;
using StabilityMatrix.Avalonia.ViewModels.Base; using StabilityMatrix.Avalonia.ViewModels.Base;
#pragma warning disable CS0657 // Not a valid attribute location for this declaration #pragma warning disable CS0657 // Not a valid attribute location for this declaration
@ -16,9 +15,9 @@ public class TestLoadableViewModel : LoadableViewModelBase
{ {
[JsonInclude] [JsonInclude]
public string? Included { get; set; } public string? Included { get; set; }
public int Id { get; set; } public int Id { get; set; }
[JsonIgnore] [JsonIgnore]
public int Ignored { get; set; } public int Ignored { get; set; }
} }
@ -43,10 +42,10 @@ public partial class TestLoadableViewModelObservable : LoadableViewModelBase
[ObservableProperty] [ObservableProperty]
[property: JsonIgnore] [property: JsonIgnore]
private string? title; private string? title;
[ObservableProperty] [ObservableProperty]
private int id; private int id;
[RelayCommand] [RelayCommand]
private void TestCommand() private void TestCommand()
{ {
@ -76,9 +75,9 @@ public class LoadableViewModelBaseTests
Id = 123, Id = 123,
Ignored = 456, Ignored = 456,
}; };
var state = vm.SaveStateToJsonObject(); var state = vm.SaveStateToJsonObject();
// [JsonInclude] and not marked property should be serialized. // [JsonInclude] and not marked property should be serialized.
// Ignored property should be ignored. // Ignored property should be ignored.
Assert.AreEqual(2, state.Count); Assert.AreEqual(2, state.Count);
@ -90,17 +89,13 @@ public class LoadableViewModelBaseTests
public void TestSaveStateToJsonObject_Observable() public void TestSaveStateToJsonObject_Observable()
{ {
// Mvvm ObservableProperty should be serialized. // Mvvm ObservableProperty should be serialized.
var vm = new TestLoadableViewModelObservable var vm = new TestLoadableViewModelObservable { Title = "abc", Id = 123, };
{
Title = "abc",
Id = 123,
};
var state = vm.SaveStateToJsonObject(); var state = vm.SaveStateToJsonObject();
// Title should be ignored since it has [JsonIgnore] // Title should be ignored since it has [JsonIgnore]
// Command should be ignored from excluded type rules // Command should be ignored from excluded type rules
// Id should be serialized // Id should be serialized
Assert.AreEqual(1, state.Count); Assert.AreEqual(1, state.Count);
Assert.AreEqual(123, state["Id"].Deserialize<int>()); Assert.AreEqual(123, state["Id"].Deserialize<int>());
} }
@ -110,23 +105,20 @@ public class LoadableViewModelBaseTests
{ {
// Properties of type IJsonLoadableState should be serialized by calling their // Properties of type IJsonLoadableState should be serialized by calling their
// SaveStateToJsonObject method. // SaveStateToJsonObject method.
// Make a mock IJsonLoadableState // Make a mock IJsonLoadableState
var mockState = new Mock<IJsonLoadableState>(); var mockState = Substitute.For<IJsonLoadableState>();
var vm = new TestLoadableViewModelNestedInterface { NestedState = mockState };
var vm = new TestLoadableViewModelNestedInterface
{
NestedState = mockState.Object
};
// Serialize // Serialize
var state = vm.SaveStateToJsonObject(); var state = vm.SaveStateToJsonObject();
// Check results // Check results
Assert.AreEqual(1, state.Count); Assert.AreEqual(1, state.Count);
// Check that SaveStateToJsonObject was called // Check that SaveStateToJsonObject was called
mockState.Verify(x => x.SaveStateToJsonObject(), Times.Once); mockState.Received().SaveStateToJsonObject();
} }
[TestMethod] [TestMethod]
@ -139,20 +131,20 @@ public class LoadableViewModelBaseTests
Id = 123, Id = 123,
Ignored = 456, Ignored = 456,
}; };
var state = vm.SaveStateToJsonObject(); var state = vm.SaveStateToJsonObject();
// Create a new instance and load the state // Create a new instance and load the state
var vm2 = new TestLoadableViewModel(); var vm2 = new TestLoadableViewModel();
vm2.LoadStateFromJsonObject(state); vm2.LoadStateFromJsonObject(state);
// Check [JsonInclude] and not marked property was loaded // Check [JsonInclude] and not marked property was loaded
Assert.AreEqual("abc", vm2.Included); Assert.AreEqual("abc", vm2.Included);
Assert.AreEqual(123, vm2.Id); Assert.AreEqual(123, vm2.Id);
// Check ignored property was not loaded // Check ignored property was not loaded
Assert.AreEqual(0, vm2.Ignored); Assert.AreEqual(0, vm2.Ignored);
} }
[TestMethod] [TestMethod]
public void TestLoadStateFromJsonObject_Nested_DefaultCtor() public void TestLoadStateFromJsonObject_Nested_DefaultCtor()
{ {
@ -163,27 +155,24 @@ public class LoadableViewModelBaseTests
Id = 123, Id = 123,
Ignored = 456, Ignored = 456,
}; };
var vm = new TestLoadableViewModelNested var vm = new TestLoadableViewModelNested { NestedState = nested };
{
NestedState = nested
};
var state = vm.SaveStateToJsonObject(); var state = vm.SaveStateToJsonObject();
// Create a new instance with null NestedState, rely on default ctor // Create a new instance with null NestedState, rely on default ctor
var vm2 = new TestLoadableViewModelNested(); var vm2 = new TestLoadableViewModelNested();
vm2.LoadStateFromJsonObject(state); vm2.LoadStateFromJsonObject(state);
// Check nested state was loaded // Check nested state was loaded
Assert.IsNotNull(vm2.NestedState); Assert.IsNotNull(vm2.NestedState);
var loadedNested = (TestLoadableViewModel) vm2.NestedState; var loadedNested = (TestLoadableViewModel)vm2.NestedState;
Assert.AreEqual("abc", loadedNested.Included); Assert.AreEqual("abc", loadedNested.Included);
Assert.AreEqual(123, loadedNested.Id); Assert.AreEqual(123, loadedNested.Id);
Assert.AreEqual(0, loadedNested.Ignored); Assert.AreEqual(0, loadedNested.Ignored);
} }
[TestMethod] [TestMethod]
public void TestLoadStateFromJsonObject_Nested_Existing() public void TestLoadStateFromJsonObject_Nested_Existing()
{ {
@ -194,63 +183,60 @@ public class LoadableViewModelBaseTests
Id = 123, Id = 123,
Ignored = 456, Ignored = 456,
}; };
var vm = new TestLoadableViewModelNestedInterface var vm = new TestLoadableViewModelNestedInterface { NestedState = nested };
{
NestedState = nested
};
var state = vm.SaveStateToJsonObject(); var state = vm.SaveStateToJsonObject();
// Create a new instance with existing NestedState // Create a new instance with existing NestedState
var vm2 = new TestLoadableViewModelNestedInterface var vm2 = new TestLoadableViewModelNestedInterface
{ {
NestedState = new TestLoadableViewModel() NestedState = new TestLoadableViewModel()
}; };
vm2.LoadStateFromJsonObject(state); vm2.LoadStateFromJsonObject(state);
// Check nested state was loaded // Check nested state was loaded
Assert.IsNotNull(vm2.NestedState); Assert.IsNotNull(vm2.NestedState);
var loadedNested = (TestLoadableViewModel) vm2.NestedState; var loadedNested = (TestLoadableViewModel)vm2.NestedState;
Assert.AreEqual("abc", loadedNested.Included); Assert.AreEqual("abc", loadedNested.Included);
Assert.AreEqual(123, loadedNested.Id); Assert.AreEqual(123, loadedNested.Id);
Assert.AreEqual(0, loadedNested.Ignored); Assert.AreEqual(0, loadedNested.Ignored);
} }
[TestMethod] [TestMethod]
public void TestLoadStateFromJsonObject_ReadOnly() public void TestLoadStateFromJsonObject_ReadOnly()
{ {
var vm = new TestLoadableViewModelReadOnly(456); var vm = new TestLoadableViewModelReadOnly(456);
var state = vm.SaveStateToJsonObject(); var state = vm.SaveStateToJsonObject();
// Check no properties were serialized // Check no properties were serialized
Assert.AreEqual(0, state.Count); Assert.AreEqual(0, state.Count);
// Create a new instance and load the state // Create a new instance and load the state
var vm2 = new TestLoadableViewModelReadOnly(123); var vm2 = new TestLoadableViewModelReadOnly(123);
vm2.LoadStateFromJsonObject(state); vm2.LoadStateFromJsonObject(state);
// Read only property should have been ignored // Read only property should have been ignored
Assert.AreEqual(123, vm2.ReadOnly); Assert.AreEqual(123, vm2.ReadOnly);
} }
[TestMethod] [TestMethod]
public void TestLoadStateFromJsonObject_ReadOnlyLoadable() public void TestLoadStateFromJsonObject_ReadOnlyLoadable()
{ {
var vm = new TestLoadableViewModelReadOnlyLoadable var vm = new TestLoadableViewModelReadOnlyLoadable
{ {
ReadOnlyLoadable = ReadOnlyLoadable = { Included = "abc-123" }
{
Included = "abc-123"
}
}; };
var state = vm.SaveStateToJsonObject(); var state = vm.SaveStateToJsonObject();
// Check readonly loadable property was serialized // Check readonly loadable property was serialized
Assert.AreEqual(1, state.Count); 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.Configuration.Json" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="7.0.8" /> <PackageReference Include="Microsoft.Extensions.Http.Polly" Version="7.0.8" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" /> <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.TestAdapter" Version="3.1.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.0.4" /> <PackageReference Include="MSTest.TestFramework" Version="3.0.4" />
<PackageReference Include="coverlet.collector" Version="6.0.0"> <PackageReference Include="coverlet.collector" Version="6.0.0">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference> </PackageReference>
<PackageReference Include="NSubstitute" Version="5.1.0" />
<PackageReference Include="Polly" Version="7.2.4" /> <PackageReference Include="Polly" Version="7.2.4" />
<PackageReference Include="Polly.Contrib.WaitAndRetry" Version="1.1.1" /> <PackageReference Include="Polly.Contrib.WaitAndRetry" Version="1.1.1" />
</ItemGroup> </ItemGroup>
@ -32,7 +32,10 @@
</ItemGroup> </ItemGroup>
<ItemGroup>
<EmbeddedResource Include="..\StabilityMatrix.Avalonia\Assets\ImagePrompt.tmLanguage.json" />
<EmbeddedResource Include="..\StabilityMatrix.Avalonia\Assets\ThemeMatrixDark.json" />
</ItemGroup>
</Project> </Project>

Loading…
Cancel
Save