More unit testing and associated refactoring
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
using ASCOM;
|
||||
using ASCOM.Astrometry;
|
||||
using System;
|
||||
using ASCOM;
|
||||
using ASCOM.Astrometry.AstroUtils;
|
||||
using ASCOM.Meade.net;
|
||||
using ASCOM.Meade.net.AstroMaths;
|
||||
@@ -34,9 +34,12 @@ namespace Meade.net.Telescope.UnitTests
|
||||
_astroUtilsMock = new Mock<IAstroUtils>();
|
||||
|
||||
_sharedResourcesWrapperMock = new Mock<ISharedResourcesWrapper>();
|
||||
_sharedResourcesWrapperMock.Setup(x => x.SendString(":GZ#")).Returns("DDD*MM’SS#");
|
||||
_sharedResourcesWrapperMock.Setup(x => x.AUTOSTAR497).Returns(() => "AUTOSTAR497");
|
||||
_sharedResourcesWrapperMock.Setup(x => x.AUTOSTAR497_31EE).Returns(() => "31EE");
|
||||
|
||||
_sharedResourcesWrapperMock.Setup(x => x.Lock(It.IsAny<Action>())).Callback<Action>(action => { action(); });
|
||||
|
||||
_sharedResourcesWrapperMock.Setup(x => x.ReadProfile()).Returns(_profileProperties);
|
||||
|
||||
_astroMathsMock = new Mock<IAstroMaths>();
|
||||
@@ -93,10 +96,12 @@ namespace Meade.net.Telescope.UnitTests
|
||||
{
|
||||
_sharedResourcesWrapperMock.Setup(x => x.ProductName).Returns(() => _sharedResourcesWrapperMock.Object.AUTOSTAR497);
|
||||
_sharedResourcesWrapperMock.Setup(x => x.FirmwareVersion).Returns(() => _sharedResourcesWrapperMock.Object.AUTOSTAR497_31EE);
|
||||
_telescope.Connected = true;
|
||||
|
||||
string expectedResult = "test result string";
|
||||
_sharedResourcesWrapperMock.Setup(x => x.SendString(It.IsAny<string>())).Returns(expectedResult);
|
||||
_sharedResourcesWrapperMock.Setup(x => x.SendString(":ED#")).Returns(expectedResult);
|
||||
_telescope.Connected = true;
|
||||
|
||||
|
||||
|
||||
var actualResult = _telescope.Action("handbox", "readdisplay");
|
||||
|
||||
@@ -127,6 +132,7 @@ namespace Meade.net.Telescope.UnitTests
|
||||
{
|
||||
_sharedResourcesWrapperMock.Setup(x => x.ProductName).Returns(() => _sharedResourcesWrapperMock.Object.AUTOSTAR497);
|
||||
_sharedResourcesWrapperMock.Setup(x => x.FirmwareVersion).Returns(() => _sharedResourcesWrapperMock.Object.AUTOSTAR497_31EE);
|
||||
|
||||
_telescope.Connected = true;
|
||||
_telescope.Action("handbox", action);
|
||||
|
||||
@@ -232,5 +238,64 @@ namespace Meade.net.Telescope.UnitTests
|
||||
_sharedResourcesWrapperMock.Verify(x => x.SendString(sendMessage), Times.Once);
|
||||
Assert.That(actualMessage, Is.EqualTo(expectedMessage));
|
||||
}
|
||||
|
||||
[TestCase(true)]
|
||||
[TestCase(false)]
|
||||
public void Connected_Get_ReturnsExpectedValue(bool expectedConnected)
|
||||
{
|
||||
_sharedResourcesWrapperMock.Setup(x => x.ProductName).Returns(() => _sharedResourcesWrapperMock.Object.AUTOSTAR497);
|
||||
_sharedResourcesWrapperMock.Setup(x => x.FirmwareVersion).Returns(() => _sharedResourcesWrapperMock.Object.AUTOSTAR497_31EE);
|
||||
_telescope.Connected = expectedConnected;
|
||||
|
||||
Assert.That(_telescope.Connected, Is.EqualTo(expectedConnected));
|
||||
}
|
||||
|
||||
|
||||
[Test]
|
||||
public void Connected_Set_SettingTrueWhenTrue_ThenDoesNothing()
|
||||
{
|
||||
_sharedResourcesWrapperMock.Setup(x => x.ProductName).Returns(() => _sharedResourcesWrapperMock.Object.AUTOSTAR497);
|
||||
_sharedResourcesWrapperMock.Setup(x => x.FirmwareVersion).Returns(() => _sharedResourcesWrapperMock.Object.AUTOSTAR497_31EE);
|
||||
_telescope.Connected = true;
|
||||
_sharedResourcesWrapperMock.Verify( x => x.Connect(It.IsAny<string>()),Times.Once);
|
||||
|
||||
//act
|
||||
_telescope.Connected = true;
|
||||
|
||||
//assert
|
||||
_sharedResourcesWrapperMock.Verify(x => x.Connect(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Connected_Set_SettingFalseWhenTrue_ThenDisconnects()
|
||||
{
|
||||
_sharedResourcesWrapperMock.Setup(x => x.ProductName).Returns(() => _sharedResourcesWrapperMock.Object.AUTOSTAR497);
|
||||
_sharedResourcesWrapperMock.Setup(x => x.FirmwareVersion).Returns(() => _sharedResourcesWrapperMock.Object.AUTOSTAR497_31EE);
|
||||
_telescope.Connected = true;
|
||||
_sharedResourcesWrapperMock.Verify(x => x.Connect(It.IsAny<string>()), Times.Once);
|
||||
|
||||
//act
|
||||
_telescope.Connected = false;
|
||||
|
||||
//assert
|
||||
_sharedResourcesWrapperMock.Verify(x => x.Disconnect(It.IsAny<string>()), Times.Once());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Connected_Set_WhenFailsToConnect_ThenDisconnects()
|
||||
{
|
||||
_sharedResourcesWrapperMock.Setup(x => x.ProductName).Returns(() => _sharedResourcesWrapperMock.Object.AUTOSTAR497);
|
||||
_sharedResourcesWrapperMock.Setup(x => x.FirmwareVersion).Returns(() => _sharedResourcesWrapperMock.Object.AUTOSTAR497_31EE);
|
||||
|
||||
_sharedResourcesWrapperMock.Setup(x => x.SendString(It.IsAny<string>())).Throws(new Exception("TestFailed"));
|
||||
|
||||
//act
|
||||
_telescope.Connected = true;
|
||||
|
||||
//assert
|
||||
_sharedResourcesWrapperMock.Verify(x => x.Disconnect(It.IsAny<string>()), Times.Once());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,7 +65,7 @@ namespace ASCOM.Meade.net
|
||||
/// <summary>
|
||||
/// Variable to hold the trace logger object (creates a diagnostic log file with information that you specify)
|
||||
/// </summary>
|
||||
internal static TraceLogger tl;
|
||||
private TraceLogger tl;
|
||||
|
||||
private readonly ISharedResourcesWrapper _sharedResourcesWrapper;
|
||||
|
||||
@@ -275,6 +275,9 @@ namespace ASCOM.Meade.net
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (Connected)
|
||||
Connected = false;
|
||||
|
||||
// Clean up the tracelogger and util objects
|
||||
tl.Enabled = false;
|
||||
tl.Dispose();
|
||||
@@ -375,6 +378,11 @@ namespace ASCOM.Meade.net
|
||||
|
||||
private void SelectSite(int site)
|
||||
{
|
||||
if (site < 1)
|
||||
throw new ArgumentOutOfRangeException("site cannot be lower than 1");
|
||||
else if (site > 4)
|
||||
throw new ArgumentOutOfRangeException("site cannot be higher than 4");
|
||||
|
||||
_sharedResourcesWrapper.SendBlind($":W{site}#");
|
||||
//:W<n>#
|
||||
//Set current site to<n>, an ASCII digit in the range 1..4
|
||||
@@ -2000,7 +2008,7 @@ namespace ASCOM.Meade.net
|
||||
/// <param name="identifier"></param>
|
||||
/// <param name="message"></param>
|
||||
/// <param name="args"></param>
|
||||
internal static void LogMessage(string identifier, string message, params object[] args)
|
||||
internal void LogMessage(string identifier, string message, params object[] args)
|
||||
{
|
||||
var msg = string.Format(message, args);
|
||||
tl.LogMessage(identifier, msg);
|
||||
|
||||
Reference in New Issue
Block a user