using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using Moq; using NucuCar.Sensors; using NucuCar.Sensors.EnvironmentSensor; using NucuCar.Sensors.Telemetry; using NucuCarSensorsProto; using Xunit; namespace NucuCar.UnitTests.NucuCar.Sensors.Tests.EnvironmentSensor.Tests { public class Bme680WorkerTest { private readonly Mock> _mockLogger; private readonly Mock> _mockOptions; private readonly Mock _mockSensorTelemetry; private readonly Mock _mockTestBme680Sensor; private readonly Mock> _mockBme680ISensor; private readonly CancellationTokenSource _cts; public Bme680WorkerTest() { _cts = new CancellationTokenSource(); _mockLogger = new Mock>(); _mockOptions = new Mock>(); _mockSensorTelemetry = new Mock(); _mockTestBme680Sensor = new Mock(); _mockBme680ISensor = new Mock>(); _mockBme680ISensor.Setup(o => o.Object).Returns(_mockTestBme680Sensor.Object); } [Fact] public async Task Test_Bme680Worker_ServiceDisabled() { _mockOptions.Setup(o => o.Value).Returns(new Bme680Config() { ServiceEnabled = false }); var service = new Bme680Worker(_mockLogger.Object, _mockOptions.Object, _mockSensorTelemetry.Object, _mockBme680ISensor.Object); await service.StartAsync(_cts.Token); _mockTestBme680Sensor.Verify(s => s.InitializeSensor(), Times.Never); } [Fact] public async Task Test_Bme680Worker_SensorIsInitialized() { _mockOptions.Setup(o => o.Value).Returns(new Bme680Config() { ServiceEnabled = true, MeasurementInterval = 1000 }); var service = new Bme680Worker(_mockLogger.Object, _mockOptions.Object, _mockSensorTelemetry.Object, _mockBme680ISensor.Object); await service.StartAsync(_cts.Token); _mockTestBme680Sensor.Verify(s => s.InitializeSensor(), Times.AtLeastOnce); await service.StopAsync(_cts.Token); } [Fact] public async Task Test_Bme680Worker_SensorIsBeingMeasured() { _mockOptions.Setup(o => o.Value).Returns(new Bme680Config() { ServiceEnabled = true, MeasurementInterval = 100 }); _mockTestBme680Sensor.Setup(s => s.GetState()).Returns(SensorStateEnum.Initialized); var service = new Bme680Worker(_mockLogger.Object, _mockOptions.Object, _mockSensorTelemetry.Object, _mockBme680ISensor.Object); await service.StartAsync(_cts.Token); _mockTestBme680Sensor.Verify(s => s.InitializeSensor(), Times.AtLeastOnce); _mockTestBme680Sensor.Verify(s => s.TakeMeasurement(), Times.AtLeastOnce); await service.StopAsync(_cts.Token); } } }