A very lightweight extension the popular C# Moq library, to be able automatically setup your test subjects. Allowing you to write tests faster with less code and more readability. Automatically mocking the parameters if a certain class's constructor parameters.
- Auto mock constructor parameters
- Be able to pass in configuration arguments to setup mocks and concrete implementations
- Be able to configure the Moq.Mock Behavior
Some examples with the imaginary Golfer class.
[Fact]
public void Golfer_should_swing_with_given_club()
{
var subject = AutoMock.Create<Golfer>();
var result = subject.Instance.Swing();
Assert.Equal("I swung a mocked club", result);
}
[Fact]
public void Golfer_should_use_concrete_caddy()
{
var subject = AutoMock.Create<Golfer>(config =>
{
config.For<ICaddy>(new Caddy());
});
var result = subject.Instance.Swing();
Assert.Equal("I swung a real club", result);
}
[Fact]
public void Golfer_should_use_the_club()
{
var subject = AutoMock.Create<Golfer>();
// .GetMock<TType>() is a built in helper function
// to get a mocked constructor argument
var clubMock = subject.GetMock<IClub>();
var result = subject.Instance.Swing();
clubMock.Verify(club => club.Use(), Times.Once);
}