More Unit Tests

This commit is contained in:
2019-09-09 22:16:51 -06:00
parent 52a566eaf4
commit d99bce4d14
12 changed files with 296 additions and 6 deletions

View File

@@ -0,0 +1,28 @@
using System;
using System.Linq.Expressions;
using Moq;
using NUnit.Framework;
using Tello_Drone;
namespace Tests
{
public class ConsoleLoggerTests
{
[SetUp]
public void Setup()
{
}
[Test]
public void LogTest()
{
var correctMessageSent = false;
var console = new ConsoleLogger();
var test = "Test";
console.LogReceived += delegate(string s) { correctMessageSent |= s == test; };
console.Log(test);
Assert.AreEqual(correctMessageSent, true);
}
}
}

View File

@@ -0,0 +1,159 @@
using System;
using System.Net.Sockets;
using ImTools;
using Moq;
using NUnit.Framework;
using Tello_Drone;
namespace Tests
{
public class DroneCommandsTests
{
private Mock<IUdpClientWrapperFactory> _mockUdpClientFactory;
private Mock<IUdpClientWrapper> _mockUdpClientWrapper;
[SetUp]
public void Setup()
{
_mockUdpClientFactory = new Mock<IUdpClientWrapperFactory>();
_mockUdpClientWrapper = new Mock<IUdpClientWrapper>();
}
[Test]
public void DownCommandTest()
{
_mockUdpClientFactory.Setup(f => f.Create()).Returns(_mockUdpClientWrapper.Object);
var droneCommands = new DroneComands(_mockUdpClientFactory.Object);
droneCommands.Down(10);
_mockUdpClientWrapper.Verify(x => x.TrySend("down 10", It.IsAny<int>(), It.IsAny<int>()), Times.Once);
}
[Test]
public void UpCommandTest()
{
_mockUdpClientFactory.Setup(f => f.Create()).Returns(_mockUdpClientWrapper.Object);
var droneCommands = new DroneComands(_mockUdpClientFactory.Object);
droneCommands.Up(10);
_mockUdpClientWrapper.Verify(x => x.TrySend("up 10", It.IsAny<int>(), It.IsAny<int>()), Times.Once);
}
[Test]
public void ForwardCommandTest()
{
_mockUdpClientFactory.Setup(f => f.Create()).Returns(_mockUdpClientWrapper.Object);
var droneCommands = new DroneComands(_mockUdpClientFactory.Object);
droneCommands.Forward(10);
_mockUdpClientWrapper.Verify(x => x.TrySend("forward 10", It.IsAny<int>(), It.IsAny<int>()), Times.Once);
}
[Test]
public void ReverseCommandTest()
{
_mockUdpClientFactory.Setup(f => f.Create()).Returns(_mockUdpClientWrapper.Object);
var droneCommands = new DroneComands(_mockUdpClientFactory.Object);
droneCommands.Reverse(10);
_mockUdpClientWrapper.Verify(x => x.TrySend("back 10", It.IsAny<int>(), It.IsAny<int>()), Times.Once);
}
[Test]
public void CommandStateTest()
{
_mockUdpClientWrapper.Setup(x => x.TrySend(It.IsAny<string>(), It.IsAny<int>(), It.IsAny<int>())).Returns(true);
_mockUdpClientFactory.Setup(f => f.Create()).Returns(_mockUdpClientWrapper.Object);
var droneCommands = new DroneComands(_mockUdpClientFactory.Object);
droneCommands.Command();
_mockUdpClientWrapper.Verify(x => x.TrySend("command", It.IsAny<int>(), It.IsAny<int>()), Times.Once);
}
[Test]
public void LeftCommandTest()
{
_mockUdpClientFactory.Setup(f => f.Create()).Returns(_mockUdpClientWrapper.Object);
var droneCommands = new DroneComands(_mockUdpClientFactory.Object);
droneCommands.Left(10);
_mockUdpClientWrapper.Verify(x => x.TrySend("left 10", It.IsAny<int>(), It.IsAny<int>()), Times.Once);
}
[Test]
public void RightCommandTest()
{
_mockUdpClientFactory.Setup(f => f.Create()).Returns(_mockUdpClientWrapper.Object);
var droneCommands = new DroneComands(_mockUdpClientFactory.Object);
droneCommands.Right(10);
_mockUdpClientWrapper.Verify(x => x.TrySend("right 10", It.IsAny<int>(), It.IsAny<int>()), Times.Once);
}
[Test]
public void LeftFlipCommandTest()
{
_mockUdpClientFactory.Setup(f => f.Create()).Returns(_mockUdpClientWrapper.Object);
var droneCommands = new DroneComands(_mockUdpClientFactory.Object);
droneCommands.LeftFlip();
_mockUdpClientWrapper.Verify(x => x.TrySend("flip l", It.IsAny<int>(), It.IsAny<int>()), Times.Once);
}
[Test]
public void RightFlipCommandTest()
{
_mockUdpClientFactory.Setup(f => f.Create()).Returns(_mockUdpClientWrapper.Object);
var droneCommands = new DroneComands(_mockUdpClientFactory.Object);
droneCommands.RightFlip();
_mockUdpClientWrapper.Verify(x => x.TrySend("flip r", It.IsAny<int>(), It.IsAny<int>()), Times.Once);
}
[Test]
public void FrontFlipCommandTest()
{
_mockUdpClientFactory.Setup(f => f.Create()).Returns(_mockUdpClientWrapper.Object);
var droneCommands = new DroneComands(_mockUdpClientFactory.Object);
droneCommands.FrontFlip();
_mockUdpClientWrapper.Verify(x => x.TrySend("flip f", It.IsAny<int>(), It.IsAny<int>()), Times.Once);
}
[Test]
public void BackFlipCommandTest()
{
_mockUdpClientFactory.Setup(f => f.Create()).Returns(_mockUdpClientWrapper.Object);
var droneCommands = new DroneComands(_mockUdpClientFactory.Object);
droneCommands.BackFlip();
_mockUdpClientWrapper.Verify(x => x.TrySend("flip b", It.IsAny<int>(), It.IsAny<int>()), Times.Once);
}
[Test]
public void InitialTakeOffCommandTest()
{
_mockUdpClientFactory.Setup(f => f.Create()).Returns(_mockUdpClientWrapper.Object);
var droneCommands = new DroneComands(_mockUdpClientFactory.Object);
droneCommands.InitialTakeOff();
_mockUdpClientWrapper.Verify(x => x.TrySend("takeoff", It.IsAny<int>(), It.IsAny<int>()), Times.Once);
}
[Test]
public void TakeOffCommandTest()
{
_mockUdpClientFactory.Setup(f => f.Create()).Returns(_mockUdpClientWrapper.Object);
var droneCommands = new DroneComands(_mockUdpClientFactory.Object);
droneCommands.TakeOff();
_mockUdpClientWrapper.Verify(x => x.TrySend("takeoff", It.IsAny<int>(), It.IsAny<int>()), Times.Once);
}
[Test]
public void LandCommandTest()
{
_mockUdpClientFactory.Setup(f => f.Create()).Returns(_mockUdpClientWrapper.Object);
var droneCommands = new DroneComands(_mockUdpClientFactory.Object);
droneCommands.Land();
_mockUdpClientWrapper.Verify(x => x.TrySend("land", It.IsAny<int>(), It.IsAny<int>()), Times.Once);
}
[Test]
public void RotateClockwiseCommandTest()
{
_mockUdpClientFactory.Setup(f => f.Create()).Returns(_mockUdpClientWrapper.Object);
var droneCommands = new DroneComands(_mockUdpClientFactory.Object);
droneCommands.RotateClockWise(180);
_mockUdpClientWrapper.Verify(x => x.TrySend("cw 180", It.IsAny<int>(), It.IsAny<int>()), Times.Once);
}
}
}

View File

@@ -0,0 +1,26 @@
using Moq;
using NUnit.Framework;
using Tello_Drone;
namespace Tests
{
public class DroneFactoryTests
{
private Mock<IDroneCommands> _mockDroneCommands;
[SetUp]
public void Setup()
{
_mockDroneCommands = new Mock<IDroneCommands>();
}
[Test]
public void CreateDroneTests()
{
var droneFactory = new DroneFactory(_mockDroneCommands.Object);
var drone = droneFactory.CreateDrone;
Assert.AreEqual(typeof(Drone), drone.GetType());
}
}
}

View File

@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Moq" Version="4.13.0" />
<PackageReference Include="nunit" Version="3.11.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.11.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Tello Drone\Tello Drone.csproj" />
</ItemGroup>
</Project>