-
Notifications
You must be signed in to change notification settings - Fork 998
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add unit test for ImageListCodeDomSerializer #12753
base: main
Are you sure you want to change the base?
Add unit test for ImageListCodeDomSerializer #12753
Conversation
/azp run |
Azure Pipelines successfully started running 1 pipeline(s). |
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #12753 +/- ##
===================================================
+ Coverage 76.03186% 76.15627% +0.12441%
===================================================
Files 3181 3193 +12
Lines 639670 640294 +624
Branches 47215 47238 +23
===================================================
+ Hits 486353 487624 +1271
+ Misses 149797 149163 -634
+ Partials 3520 3507 -13
Flags with carried forward coverage won't be shown. Click here to find out more. |
var baseSerializerMock = new Mock<CodeDomSerializer>(); | ||
|
||
managerMock.Setup(m => m.GetSerializer(typeof(Component), typeof(CodeDomSerializer))) | ||
.Returns(baseSerializerMock.Object); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use 4 spaces to indent code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you want to align dots, you can do this -
managerMock
.Setup(m => m.GetSerializer(typeof(Component), typeof(CodeDomSerializer)))
.Returns(baseSerializerMock.Object);
[Fact] | ||
public void Deserialize_ShouldCallBaseSerializer_WhenBaseSerializerFound() | ||
{ | ||
var managerMock = new Mock<IDesignerSerializationManager>(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please declare variables with their specifc types, e.g.:
Mock<IDesignerSerializationManager> managerMock = new();
Mock<CodeDomSerializer> baseSerializerMock = new();
This happens other times in this PR's code.
var baseSerializerMock = new Mock<CodeDomSerializer>(); | ||
|
||
managerMock.Setup(m => m.GetSerializer(typeof(Component), typeof(CodeDomSerializer))) | ||
.Returns(baseSerializerMock.Object); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test doesn't correctly trigger the expected codepath to return null
. The Deserialize
method only returns null
by chance, making the test pass incorrectly:
To ensure the test executes the intended codepath, the mock setup should return false
:
Additionally, since the correct code path contains a Debug.Fail()
call, you'll need to clear the Trace.Listeners
to prevent the test from halting prematurely. Here's the corrected test code:
[Fact]
public void Deserialize_ShouldReturnNull_WhenBaseSerializerNotFound()
{
Mock<IDesignerSerializationManager> managerMock = new();
Mock<CodeDomSerializer> baseSerializerMock = new();
managerMock.Setup(m => m.GetSerializer(typeof(Component), typeof(CodeDomSerializer)))
.Returns(false);
Trace.Listeners.Clear();
object codeObject = new();
object? result = _serializer.Deserialize(managerMock.Object, codeObject);
result.Should().BeNull();
}
|
||
result.Should().BeNull(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could increase code coverage a bit by adding a test case to the Serialize
method like this:
[Fact]
public void Serialize_ShouldGenerateCodeStatements_ForImageListWithKeys()
{
Mock<IDesignerSerializationManager> managerMock = new();
Mock<CodeDomSerializer> baseSerializerMock = new();
ImageList imageList = new();
imageList.Images.Add("Key1", new Bitmap(1, 1));
imageList.Images.Add("Key2", new Bitmap(1, 1));
baseSerializerMock.Setup(b => b.Serialize(managerMock.Object, imageList)).Returns(new CodeStatementCollection());
managerMock.Setup(m => m.GetSerializer(typeof(Component), typeof(CodeDomSerializer)))
.Returns(baseSerializerMock.Object);
ContextStack contextStack = new();
contextStack.Append(new ImageListCodeDomSerializer());
managerMock.Setup(m => m.Context).Returns(contextStack);
object? result = _serializer.Serialize(managerMock.Object, imageList);
result.Should().BeOfType<CodeStatementCollection>();
}
Related #10773
Proposed changes
Microsoft Reviewers: Open in CodeFlow