From 94bfc5a70568373852715b10d5684eb8b21e5e17 Mon Sep 17 00:00:00 2001 From: "zhengxuan.zhang" Date: Wed, 15 Jul 2026 09:57:19 +0800 Subject: [PATCH] =?UTF-8?q?fix(tests):=20=E4=BF=AE=E5=A4=8D=20SimulatedXRa?= =?UTF-8?q?ySourceTests=20=E5=9B=A0=20VariablesConnectedEvent=20=E6=9C=AA?= =?UTF-8?q?=E8=A2=AB=20Mock=20=E5=AF=BC=E8=87=B4=E7=9A=84=E5=A4=B1?= =?UTF-8?q?=E8=B4=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit SimulatedXRaySource.ConnectVariables() 中新增的 VariablesConnectedEvent 发布未被测试的 EventAggregator Mock 覆盖,导致 GetEvent() 返回 null, 触发 NullReferenceException,5 个测试全部失败。 - 补充 VariablesConnectedEvent、StatusUpdatedEvent 的 Mock 设置 - Initialize_SetsInitializedTrue 增加 VariablesConnectedEvent(true) 发布断言 - 修正 TurnOff_PublishesClosedEvent 断言逻辑:Arrange 阶段 ConnectVariables/TurnOn 已产生一次 Closed 事件,需在 Act 前清空 Invocations 避免误判发布次数 --- .../Hardware/SimulatedXRaySourceTests.cs | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/XplorePlane.Tests/Hardware/SimulatedXRaySourceTests.cs b/XplorePlane.Tests/Hardware/SimulatedXRaySourceTests.cs index 508c967e..31e09180 100644 --- a/XplorePlane.Tests/Hardware/SimulatedXRaySourceTests.cs +++ b/XplorePlane.Tests/Hardware/SimulatedXRaySourceTests.cs @@ -13,6 +13,8 @@ namespace XplorePlane.Tests.Hardware private readonly Mock _mockEventAggregator; private readonly Mock _mockLogger; private readonly Mock _mockStatusEvent; + private readonly Mock _mockVariablesConnectedEvent; + private readonly Mock _mockStatusUpdatedEvent; private readonly SimulatedXRaySource _source; public SimulatedXRaySourceTests() @@ -20,6 +22,8 @@ namespace XplorePlane.Tests.Hardware _mockEventAggregator = new Mock(); _mockLogger = new Mock(); _mockStatusEvent = new Mock(); + _mockVariablesConnectedEvent = new Mock(); + _mockStatusUpdatedEvent = new Mock(); // Setup logger to return itself for ForModule() _mockLogger.Setup(l => l.ForModule()).Returns(_mockLogger.Object); @@ -29,6 +33,18 @@ namespace XplorePlane.Tests.Hardware .Setup(ea => ea.GetEvent()) .Returns(_mockStatusEvent.Object); + // ConnectVariables() 会发布 VariablesConnectedEvent,需要 Mock 该事件避免 NullReferenceException + // ConnectVariables() publishes VariablesConnectedEvent, must mock it to avoid NullReferenceException + _mockEventAggregator + .Setup(ea => ea.GetEvent()) + .Returns(_mockVariablesConnectedEvent.Object); + + // TurnOn/TurnOff/SetVoltage/SetCurrent 会发布 StatusUpdatedEvent,同样需要 Mock + // TurnOn/TurnOff/SetVoltage/SetCurrent publish StatusUpdatedEvent, also needs mocking + _mockEventAggregator + .Setup(ea => ea.GetEvent()) + .Returns(_mockStatusUpdatedEvent.Object); + _source = new SimulatedXRaySource(_mockEventAggregator.Object, _mockLogger.Object); } @@ -43,6 +59,10 @@ namespace XplorePlane.Tests.Hardware Assert.True(initResult.Success); Assert.True(connectResult.Success); Assert.True(_source.IsConnected); + + // 验证 ConnectVariables 发布了 VariablesConnectedEvent(true) + // Verify ConnectVariables published VariablesConnectedEvent(true) + _mockVariablesConnectedEvent.Verify(e => e.Publish(true), Times.Once); } [Fact] @@ -70,6 +90,11 @@ namespace XplorePlane.Tests.Hardware _source.ConnectVariables(); _source.TurnOn(); + // ConnectVariables/TurnOn 阶段已经发布过 Closed/Opened 事件,清空调用记录避免影响 Act 阶段的断言 + // ConnectVariables/TurnOn already published Closed/Opened events during Arrange; clear invocations + // so the Act-phase assertion only counts the publish triggered by TurnOff() + _mockStatusEvent.Invocations.Clear(); + // Act var result = _source.TurnOff();