Skip to content

Commit 8bd8fb9

Browse files
committed
Fix reflection bug in GetComponentData tests
- Replace incorrect reflection-based field access with proper dictionary key access - GetComponentData() returns Dictionary<string, object> where 'properties' is a key, not a field - Tests now properly access the properties dictionary and run assertions - Fixes ineffective tests that were silently failing due to reflection returning null
1 parent 4e4d25f commit 8bd8fb9

File tree

1 file changed

+12
-24
lines changed

1 file changed

+12
-24
lines changed

TestProjects/UnityMCPTests/Assets/Tests/EditMode/Tools/ManageGameObjectTests.cs

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -461,19 +461,13 @@ public void GetComponentData_UsesSharedMaterialInEditMode()
461461
// Assert - Verify that the material property was accessed without instantiation
462462
Assert.IsNotNull(result, "GetComponentData should return a result");
463463

464-
// Tolerate varying shapes - if properties exist and are a dictionary, ensure keys are reasonable
465-
var resultType = result.GetType();
466-
var propertiesField = resultType.GetField("properties");
467-
if (propertiesField != null)
464+
// Check that result is a dictionary with properties key
465+
if (result is Dictionary<string, object> resultDict &&
466+
resultDict.TryGetValue("properties", out var propertiesObj) &&
467+
propertiesObj is Dictionary<string, object> properties)
468468
{
469-
var properties = propertiesField.GetValue(result);
470-
Assert.IsNotNull(properties, "properties should not be null when present");
471-
var dict = properties as Dictionary<string, object>;
472-
if (dict != null)
473-
{
474-
Assert.IsTrue(dict.ContainsKey("material") || dict.ContainsKey("sharedMaterial"),
475-
"Serialized data should include 'material' or 'sharedMaterial' when present.");
476-
}
469+
Assert.IsTrue(properties.ContainsKey("material") || properties.ContainsKey("sharedMaterial"),
470+
"Serialized data should include 'material' or 'sharedMaterial' when present.");
477471
}
478472

479473
// Clean up
@@ -501,19 +495,13 @@ public void GetComponentData_UsesSharedMeshInEditMode()
501495
// Assert - Verify that the mesh property was accessed without instantiation
502496
Assert.IsNotNull(result, "GetComponentData should return a result");
503497

504-
// Tolerate varying shapes - if properties exist and are a dictionary, ensure keys are reasonable
505-
var resultType = result.GetType();
506-
var propertiesField = resultType.GetField("properties");
507-
if (propertiesField != null)
498+
// Check that result is a dictionary with properties key
499+
if (result is Dictionary<string, object> resultDict &&
500+
resultDict.TryGetValue("properties", out var propertiesObj) &&
501+
propertiesObj is Dictionary<string, object> properties)
508502
{
509-
var properties = propertiesField.GetValue(result);
510-
Assert.IsNotNull(properties, "properties should not be null when present");
511-
var dict = properties as Dictionary<string, object>;
512-
if (dict != null)
513-
{
514-
Assert.IsTrue(dict.ContainsKey("mesh") || dict.ContainsKey("sharedMesh"),
515-
"Serialized data should include 'mesh' or 'sharedMesh' when present.");
516-
}
503+
Assert.IsTrue(properties.ContainsKey("mesh") || properties.ContainsKey("sharedMesh"),
504+
"Serialized data should include 'mesh' or 'sharedMesh' when present.");
517505
}
518506

519507
// Clean up

0 commit comments

Comments
 (0)