using System;
using System.Threading.Tasks;
using NSubstitute;
using NUnit.Framework;
using ShareeBike.Model.Bikes.BikeInfoNS.BluetoothLock;
using ShareeBike.Services.BluetoothLock;
using ShareeBike.Services.BluetoothLock.Tdo;
using static ShareeBike.Model.Bikes.BikeInfoNS.BluetoothLock.Command.DisconnectCommand;
using ShareeBike.Services.BluetoothLock.Exception;
namespace SharedBusinessLogic.Tests.Model.Bikes.BikeInfoNS.BluetoothLock.Command
{
[TestFixture]
public class TestDisconnectCommand
{
///
/// Use case: Disconnect lock
///
[Test]
public async Task TestDisconnect()
{
var bike = Substitute.For();
var locks = Substitute.For();
var listener = Substitute.For();
bike.LockInfo.Id.Returns(0);
bike.LockInfo.State.Returns(LockingState.Closed);
await locks.DisconnectAsync(Arg.Any(), Arg.Any());
await InvokeAsync(
bike,
locks,
listener);
Assert.That(
bike.LockInfo.State,
Is.EqualTo(LockingState.UnknownDisconnected));
// Verify behavior
Received.InOrder(async () =>
{
listener.Received().ReportStep(Step.DisconnectLock);
await locks.DisconnectAsync(Arg.Any(), Arg.Any());
});
}
///
/// Use case: Disconnect lock
///
[Test]
public async Task TestDisconnectException()
{
var bike = Substitute.For();
var locks = Substitute.For();
var listener = Substitute.For();
bike.LockInfo.Id.Returns(0);
bike.LockInfo.State.Returns(LockingState.Closed);
locks.DisconnectAsync(Arg.Any(), Arg.Any())
.Returns(x => throw new Exception("exception"));
await InvokeAsync(
bike,
locks,
listener);
Assert.That(
bike.LockInfo.State,
Is.EqualTo(LockingState.Closed));
// Verify behavior
Received.InOrder(async () =>
{
listener.Received().ReportStep(Step.DisconnectLock);
await locks.DisconnectAsync(Arg.Any(), Arg.Any());
await listener.Received().ReportStateAsync(State.GeneralDisconnectError,"exception");
});
}
}
}