Recently, I wanted to test if my HostConfigurator set-up method would correctly set-up the host configurator to run as a local system account (by calling RunAsLocalSystem on it). I tried to write a unit test for this, but this failed due to not being able to access the actual account type passed to the RunAsServiceAccountHostConfigurator instance created when the RunAsLocalSystem extension method was called on my HostConfigurator.
The solution was simple: replace the read-only private field _accountType with a read-only public property. This now allowed me to write the desired unit test:
[Fact]
public void RunAsLocalSystemAddsRunAsServiceAccountHostConfiguratorWithLocalSystemAsServiceAccount()
{
// Arrange
var hostConfiguratorMock = new Mock<HostConfigurator>();
// Act
hostConfiguratorMock.Object.RunAsLocalSystem();
// Assert
hostConfiguratorMock.Verify(h => h.AddConfigurator(It.Is<RunAsServiceAccountHostConfigurator>(c => c.AccountType == ServiceAccount.LocalSystem)), Times.Once);
}
I have also gone through the other configurator classes and done the same thing. This will also make them testable.
Recently, I wanted to test if my
HostConfigurator
set-up method would correctly set-up the host configurator to run as a local system account (by callingRunAsLocalSystem
on it). I tried to write a unit test for this, but this failed due to not being able to access the actual account type passed to theRunAsServiceAccountHostConfigurator
instance created when theRunAsLocalSystem
extension method was called on myHostConfigurator
.The solution was simple: replace the read-only private field
_accountType
with a read-only public property. This now allowed me to write the desired unit test:I have also gone through the other configurator classes and done the same thing. This will also make them testable.