NucuCar/NucuCar.UnitTests/NucuCar.Sensors.Tests/Bme680WorkerTest.cs

70 lines
2.9 KiB
C#
Raw Normal View History

2019-12-23 22:32:12 +00:00
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Moq;
2020-08-01 15:07:13 +00:00
using NucuCar.Sensors.Abstractions;
2021-08-02 18:13:59 +00:00
using NucuCar.Sensors.Modules.BME680;
2020-08-01 15:32:45 +00:00
using NucuCar.UnitTests.NucuCar.Sensors.Tests.EnvironmentSensor;
2019-12-23 22:32:12 +00:00
using Xunit;
2020-08-01 15:32:45 +00:00
namespace NucuCar.UnitTests.NucuCar.Sensors.Tests
2019-12-23 22:32:12 +00:00
{
2020-08-01 15:32:45 +00:00
/// <summary>
/// Tests the functionality of the SensorWorker; Since the class is quite generic the test uses the
/// Bme680Worker as an example.
/// </summary>
2019-12-23 22:32:12 +00:00
public class Bme680WorkerTest
{
private readonly Mock<ILogger<Bme680Worker>> _mockLogger;
private readonly Mock<IOptions<Bme680Config>> _mockOptions;
private readonly Mock<global::NucuCar.Telemetry.TelemetryPublisherProxy> _mockSensorTelemetry;
2019-12-23 22:32:12 +00:00
private readonly Mock<TestBme680Sensor> _mockTestBme680Sensor;
private readonly Mock<ISensor<Bme680Sensor>> _mockBme680ISensor;
private readonly CancellationTokenSource _cts;
public Bme680WorkerTest()
{
_cts = new CancellationTokenSource();
_mockLogger = new Mock<ILogger<Bme680Worker>>();
_mockOptions = new Mock<IOptions<Bme680Config>>();
_mockSensorTelemetry = new Mock<global::NucuCar.Telemetry.TelemetryPublisherProxy>();
2019-12-23 22:32:12 +00:00
_mockTestBme680Sensor = new Mock<TestBme680Sensor>();
_mockBme680ISensor = new Mock<ISensor<Bme680Sensor>>();
_mockBme680ISensor.Setup(o => o.Object).Returns(_mockTestBme680Sensor.Object);
}
[Fact]
public async Task Test_Bme680Worker_SensorIsInitialized()
{
_mockOptions.Setup(o => o.Value).Returns(new Bme680Config()
{
Enabled = true,
});
2021-04-24 18:44:54 +00:00
var service = new Bme680Worker(_mockLogger.Object, _mockSensorTelemetry.Object, _mockBme680ISensor.Object,
new OptionsWrapper<Bme680Config>(new Bme680Config()));
2019-12-23 22:32:12 +00:00
await service.StartAsync(_cts.Token);
_mockTestBme680Sensor.Verify(s => s.Initialize(), Times.AtLeastOnce);
await service.StopAsync(_cts.Token);
}
2021-04-24 18:44:54 +00:00
2019-12-23 22:32:12 +00:00
[Fact]
public async Task Test_Bme680Worker_SensorIsBeingMeasured()
{
_mockOptions.Setup(o => o.Value).Returns(new Bme680Config()
{
Enabled = true,
});
_mockTestBme680Sensor.Setup(s => s.GetState()).Returns(SensorStateEnum.Initialized);
2021-04-24 18:44:54 +00:00
var service = new Bme680Worker(_mockLogger.Object, _mockSensorTelemetry.Object, _mockBme680ISensor.Object,
new OptionsWrapper<Bme680Config>(new Bme680Config()));
2019-12-23 22:32:12 +00:00
await service.StartAsync(_cts.Token);
_mockTestBme680Sensor.Verify(s => s.Initialize(), Times.AtLeastOnce);
_mockTestBme680Sensor.Verify(s => s.TakeMeasurementAsync(), Times.AtLeastOnce);
await service.StopAsync(_cts.Token);
}
}
}