using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using Moq; using NucuCar.Sensors; using NucuCar.Sensors.Abstractions; using NucuCar.Sensors.Environment; using NucuCarSensorsProto; using Xunit; namespace NucuCar.UnitTests.NucuCar.Sensors.Tests.EnvironmentSensor.Tests { public class Bme680GrpcServiceTest { private readonly Mock> _mockLogger; private readonly Mock> _mockSensor; private readonly Mock> _mockOptions; private readonly Mock _mockTestSensor; public Bme680GrpcServiceTest() { _mockLogger = new Mock>(); _mockSensor = new Mock>(); _mockOptions = new Mock>(); _mockTestSensor = new Mock(); _mockOptions.Setup(mo => mo.Value).Returns(new Bme680Config() { Grpc = true, Telemetry = true, Enabled = true }); _mockSensor.Setup(ms => ms.Object).Returns(_mockTestSensor.Object); } [Fact] public void Test_GetSensorMeasurement() { _mockTestSensor.Setup(s => s.GetMeasurement()).Returns(new NucuCarSensorResponse()); var service = new Bme680GrpcService(_mockLogger.Object, _mockSensor.Object, _mockOptions.Object); service.GetMeasurement(null, null); // Verify that the sensor get measurement method is called. _mockSensor.Verify(s => s.Object.GetMeasurement(), Times.AtLeastOnce()); } [Fact] public void Test_GetSensorMeasurement_Disabled() { _mockTestSensor.Setup(s => s.GetMeasurement()).Returns(new NucuCarSensorResponse()); var options = new Mock>(); options.Setup(o => o.Value).Returns(new Bme680Config { Enabled = true, Telemetry = true, Grpc = false }); var service = new Bme680GrpcService(_mockLogger.Object, _mockSensor.Object, options.Object); var result = service.GetMeasurement(null, null); // Verify that the sensor get measurement method is not called. _mockSensor.Verify(s => s.Object.GetMeasurement(), Times.Never()); Assert.Equal(SensorStateEnum.GrpcDisabled, result.Result.State); } } }