From d8d237a1f73f33b8f37423b4e6e91858b9e50e5c Mon Sep 17 00:00:00 2001 From: "Ricardo Bossan (BEYONDSOFT CONSULTING INC) (from Dev Box)" Date: Wed, 25 Sep 2024 12:33:19 -0300 Subject: [PATCH] Replaces constructor with method to initialize dependencies, as not all methods use them Related #10773 ## Proposed changes - Adds code coverage to `FormatStringEditor` ## Customer Impact - None ## Regression? - No ## Risk - Minimal ## Test methodology - Unit tests ## Test environment(s) - 9.0.100-rc.1.24452.12 --- .../Forms/Design/FormatStringEditorTests.cs | 56 ++++++++++--------- 1 file changed, 31 insertions(+), 25 deletions(-) diff --git a/src/System.Windows.Forms.Design/tests/UnitTests/System/Windows/Forms/Design/FormatStringEditorTests.cs b/src/System.Windows.Forms.Design/tests/UnitTests/System/Windows/Forms/Design/FormatStringEditorTests.cs index 2ad6cb0ec05..6b3bf8b78f1 100644 --- a/src/System.Windows.Forms.Design/tests/UnitTests/System/Windows/Forms/Design/FormatStringEditorTests.cs +++ b/src/System.Windows.Forms.Design/tests/UnitTests/System/Windows/Forms/Design/FormatStringEditorTests.cs @@ -14,13 +14,13 @@ namespace System.Windows.Forms.Design.Tests; public class FormatStringEditorTests { private readonly FormatStringEditor _editor = new(); - private readonly Mock _mockEditorService; - private readonly Mock _mockChangeService; - private readonly Mock _provider; - private readonly Mock _context; - private readonly DataGridViewCellStyle _cellStyle; + private Mock? _mockEditorService; + private Mock? _mockChangeService; + private Mock? _provider; + private Mock? _context; + private DataGridViewCellStyle? _cellStyle; - public FormatStringEditorTests() + public void ArrangeDependencies() { _mockEditorService = new(); _mockChangeService = new(); @@ -45,62 +45,68 @@ public void EditValue_InvalidProvider_ReturnsValue(IServiceProvider provider, ob [CommonMemberData(typeof(CommonTestHelperEx), nameof(CommonTestHelperEx.GetITypeDescriptorContextTestData))] public void GetEditStyle_Invoke_ReturnsModal(ITypeDescriptorContext context) { - _editor.GetEditStyle(context).Should().Be(UITypeEditorEditStyle.Modal); + _editor.GetEditStyle(context).Should().Be(UITypeEditorEditStyle.Modal); } [WinFormsFact] public void EditValue_ShowsDialogAndUpdatesValue() { - object? result = _editor.EditValue(_context.Object, _provider.Object, "NewValue"); + ArrangeDependencies(); + + object? result = _editor.EditValue(_context?.Object, _provider!.Object, "NewValue"); result.Should().Be("NewValue"); - _mockEditorService.Verify(es => es.ShowDialog(It.IsAny()), Times.Once); + _mockEditorService?.Verify(es => es.ShowDialog(It.IsAny()), Times.Once); } [WinFormsFact] public void EditValue_CallsOnComponentChanging_Once() { - _editor.EditValue(_context.Object, _provider.Object, "NewValue"); + ArrangeDependencies(); + + _editor.EditValue(_context?.Object, _provider!.Object, "NewValue"); - _mockChangeService.Verify( - cs => cs.OnComponentChanging(_cellStyle, TypeDescriptor.GetProperties(_cellStyle)["Format"]), + _mockChangeService?.Verify( + cs => cs.OnComponentChanging(_cellStyle!, TypeDescriptor.GetProperties(_cellStyle!)["Format"]), Times.Once); - _mockChangeService.Verify( - cs => cs.OnComponentChanging(_cellStyle, TypeDescriptor.GetProperties(_cellStyle)["NullValue"]), + _mockChangeService?.Verify( + cs => cs.OnComponentChanging(_cellStyle!, TypeDescriptor.GetProperties(_cellStyle!)["NullValue"]), Times.Once); - _mockChangeService.Verify( - cs => cs.OnComponentChanging(_cellStyle, TypeDescriptor.GetProperties(_cellStyle)["FormatProvider"]), + _mockChangeService?.Verify( + cs => cs.OnComponentChanging(_cellStyle!, TypeDescriptor.GetProperties(_cellStyle!)["FormatProvider"]), Times.Once); } [WinFormsFact] public void EditValue_WithDirtyDialog_CallsOnComponentChanged_Once() { + ArrangeDependencies(); + dynamic? dialog = _editor.TestAccessor().Dynamic._formatStringDialog; - dialog = new FormatStringDialog(_context.Object); + dialog = new FormatStringDialog(_context?.Object); var dialogField = typeof(FormatStringEditor).GetField("_formatStringDialog", Reflection.BindingFlags.NonPublic | Reflection.BindingFlags.Instance); - dialogField?.SetValue(_editor, dialog); + dialogField?.SetValue(_editor, dialog); var dirtyField = typeof(FormatStringDialog).GetField("_dirty", Reflection.BindingFlags.NonPublic | Reflection.BindingFlags.Instance); dirtyField?.SetValue(dialog, true); - _editor.EditValue(_context.Object, _provider.Object, "NewValue"); + _editor.EditValue(_context?.Object, _provider!.Object!, "NewValue"); - _mockChangeService.Verify( - cs => cs.OnComponentChanged(_cellStyle, TypeDescriptor.GetProperties(_cellStyle)["Format"], null, null), + _mockChangeService?.Verify( + cs => cs.OnComponentChanged(_cellStyle!, TypeDescriptor.GetProperties(_cellStyle!)["Format"], null, null), Times.Once); - _mockChangeService.Verify( - cs => cs.OnComponentChanged(_cellStyle, TypeDescriptor.GetProperties(_cellStyle)["NullValue"], null, null), + _mockChangeService?.Verify( + cs => cs.OnComponentChanged(_cellStyle!, TypeDescriptor.GetProperties(_cellStyle!)["NullValue"], null, null), Times.Once); - _mockChangeService.Verify( - cs => cs.OnComponentChanged(_cellStyle, TypeDescriptor.GetProperties(_cellStyle)["FormatProvider"], null, null), + _mockChangeService?.Verify( + cs => cs.OnComponentChanged(_cellStyle!, TypeDescriptor.GetProperties(_cellStyle!)["FormatProvider"], null, null), Times.Once); } }