phatboyg / Topshelf

An easy service hosting framework for building Windows services using .NET
http://topshelf-project.com/
Apache License 2.0
170 stars 1 forks source link

Improved testability of several host configurators #25

Closed ErikSchierboom closed 11 years ago

ErikSchierboom commented 11 years ago

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.

phatboyg commented 11 years ago

Done.

phatboyg commented 11 years ago

Missed the 3.5 build conditional, I fixed it.