Skip to content

Commit e424e35

Browse files
CopilotMrHinsh
andcommitted
Fix NullReferenceException in test mock creation
Updated CreateMockExternalLink to handle cases where RegisteredLinkType constructors may have changed. The method now tries multiple constructor signatures and provides better error handling when reflection fails to find a suitable constructor. Co-authored-by: MrHinsh <[email protected]>
1 parent c1bb30b commit e424e35

File tree

1 file changed

+46
-2
lines changed

1 file changed

+46
-2
lines changed

src/MigrationTools.Clients.TfsObjectModel.Tests/Tools/TfsGitRepositoryInfoTests.cs

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,58 @@ public class TfsGitRepositoryInfoTests
1616
/// </summary>
1717
private ExternalLink CreateMockExternalLink(string uri)
1818
{
19-
// Create a mock RegisteredLinkType using reflection
19+
// Try to create a mock RegisteredLinkType using reflection
20+
// First try the two-parameter constructor
2021
var linkTypeConstructor = typeof(RegisteredLinkType).GetConstructor(
2122
BindingFlags.NonPublic | BindingFlags.Instance,
2223
null,
2324
new Type[] { typeof(string), typeof(string) },
2425
null);
2526

26-
var linkType = (RegisteredLinkType)linkTypeConstructor.Invoke(new object[] { "MockLinkType", "Mock Link Type" });
27+
RegisteredLinkType linkType = null;
28+
29+
if (linkTypeConstructor != null)
30+
{
31+
linkType = (RegisteredLinkType)linkTypeConstructor.Invoke(new object[] { "MockLinkType", "Mock Link Type" });
32+
}
33+
else
34+
{
35+
// Try alternative constructors if the two-parameter one doesn't exist
36+
var constructors = typeof(RegisteredLinkType).GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);
37+
38+
foreach (var ctor in constructors)
39+
{
40+
var parameters = ctor.GetParameters();
41+
if (parameters.Length == 1 && parameters[0].ParameterType == typeof(string))
42+
{
43+
linkType = (RegisteredLinkType)ctor.Invoke(new object[] { "MockLinkType" });
44+
break;
45+
}
46+
else if (parameters.Length == 0)
47+
{
48+
linkType = (RegisteredLinkType)ctor.Invoke(new object[] { });
49+
break;
50+
}
51+
}
52+
53+
// If still null, try to find any constructor with minimal parameters
54+
if (linkType == null && constructors.Length > 0)
55+
{
56+
var simplestCtor = constructors[0];
57+
var ctorParams = simplestCtor.GetParameters();
58+
var args = new object[ctorParams.Length];
59+
for (int i = 0; i < ctorParams.Length; i++)
60+
{
61+
args[i] = ctorParams[i].ParameterType.IsValueType ? Activator.CreateInstance(ctorParams[i].ParameterType) : null;
62+
}
63+
linkType = (RegisteredLinkType)simplestCtor.Invoke(args);
64+
}
65+
}
66+
67+
if (linkType == null)
68+
{
69+
throw new InvalidOperationException("Could not create RegisteredLinkType instance via reflection. Available constructors may have changed.");
70+
}
2771

2872
// Create ExternalLink using the mock link type
2973
return new ExternalLink(linkType, uri);

0 commit comments

Comments
 (0)