mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-06 19:26:39 +02:00
User/ryanbod/mock settings disk access (#6188)
* 1) Making Directory Methods private. 2) Removing the CreateDirectory / DeleteDirectory functionality from all Settings Unit Tests. * Abstracting disk access via IIOProvider to be able to provide mocks for unit tests instead of writing to disk. This also prevents developers who are running unit tests from interfering with the PowerToys settings on their local dev box. * Dependency Injecting stub SettingsUtils for all tests * Removing ISettingsUtils from constructors of objects that need to be deserialized (ColorPickerSettings/PowerLauncherSettings) as this breaks System.Text.Json * Removing unused namespace reference * Removing redifined mock * As per PR feedback. Stub Settings utils should work with any settings type if the intent is to compile / avoid null ref exceptions. Strangely when implementing this fix it became apparent that a stub settings isn't enough, and disk access needed to be mocked. I can't explain why the tests were passing previously. * Leveraging GetMockIOProviderForSaveLoadExists
This commit is contained in:
committed by
GitHub
parent
6e89ef62e4
commit
0f6428eed0
@@ -4,8 +4,11 @@
|
||||
|
||||
using System;
|
||||
using Microsoft.PowerToys.Settings.UI.Lib;
|
||||
using Microsoft.PowerToys.Settings.UI.Lib.Utilities;
|
||||
using Microsoft.PowerToys.Settings.UI.UnitTests.Mocks;
|
||||
using Microsoft.PowerToys.Settings.UnitTest;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Moq;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Newtonsoft.Json.Schema;
|
||||
|
||||
@@ -21,6 +24,13 @@ namespace CommonLibTest
|
||||
[Obsolete]
|
||||
public void ToJsonString_ShouldReturnValidJSONOfModel_WhenSuccessful()
|
||||
{
|
||||
//Mock Disk access
|
||||
string saveContent = string.Empty;
|
||||
string savePath = string.Empty;
|
||||
var mockIOProvider = IIOProviderMocks.GetMockIOProviderForSaveLoadExists();
|
||||
|
||||
var settingsUtils = new SettingsUtils(mockIOProvider.Object);
|
||||
|
||||
// Arrange
|
||||
string file_name = "test\\BasePTModuleSettingsTest";
|
||||
string expectedSchemaText = @"
|
||||
@@ -39,11 +49,11 @@ namespace CommonLibTest
|
||||
}";
|
||||
|
||||
string testSettingsConfigs = new BasePTSettingsTest().ToJsonString();
|
||||
SettingsUtils.SaveSettings(testSettingsConfigs, file_name);
|
||||
settingsUtils.SaveSettings(testSettingsConfigs, file_name);
|
||||
JsonSchema expectedSchema = JsonSchema.Parse(expectedSchemaText);
|
||||
|
||||
// Act
|
||||
JObject actualSchema = JObject.Parse(SettingsUtils.GetSettings<BasePTSettingsTest>(file_name).ToJsonString());
|
||||
JObject actualSchema = JObject.Parse(settingsUtils.GetSettings<BasePTSettingsTest>(file_name).ToJsonString());
|
||||
bool valid = actualSchema.IsValid(expectedSchema);
|
||||
|
||||
// Assert
|
||||
|
||||
Reference in New Issue
Block a user