Skip to content

Instantly share code, notes, and snippets.

@lkaczanowski
Last active March 29, 2017 17:34
Show Gist options
  • Save lkaczanowski/258ef8235f51a7a9e725b816ca89a75c to your computer and use it in GitHub Desktop.
Save lkaczanowski/258ef8235f51a7a9e725b816ca89a75c to your computer and use it in GitHub Desktop.
Testing with AutoFixture
public interface IDependencyA
{
int QueryMethod(int q);
}
public interface IDependencyB
{
void CommandMethod(int c);
}
public interface IComponentWithConstructorInjection
{
IDependencyA DependencyA { get; }
IDependencyB DependencyB { get; }
}
public class ComponentWithConstructorInjection : IComponentWithConstructorInjection
{
public ComponentWithConstructorInjection(IDependencyA dependencyA, IDependencyB dependencyB)
{
DependencyA = dependencyA;
DependencyB = dependencyB;
}
public IDependencyA DependencyA { get; }
public IDependencyB DependencyB { get; }
public int DoSomething()
{
DependencyB.CommandMethod(4);
return DependencyA.QueryMethod(4);
}
}
[TestFixture]
public class ComponentWithConstructorInjectionTests
{
private ComponentWithConstructorInjection _sut;
private IFixture _mockFixture;
private Mock<IDependencyA> _dependencyAMock;
private Mock<IDependencyB> _dependencyBMock;
[SetUp]
public void Setup()
{
_mockFixture = new Fixture().Customize(new AutoMoqCustomization());
_mockFixture.Customize<ComponentWithConstructorInjection>(c => c.FromFactory(new MethodInvoker(new GreedyConstructorQuery())));
_dependencyAMock = _mockFixture.Freeze<Mock<IDependencyA>>();
_dependencyBMock = _mockFixture.Freeze<Mock<IDependencyB>>();
_sut = _mockFixture.Create<ComponentWithConstructorInjection>();
}
[Test]
public void SomeTest()
{
// arrange
int expected = 10;
_dependencyAMock.Setup(x => x.QueryMethod(4)).Returns(expected);
// act
var actual = _sut.DoSomething();
// assert
Assert.AreEqual(expected, actual);
_dependencyBMock.Verify(x => x.CommandMethod(4));
}
[Test(Description = "Z tymi testami freezowanie nie jest potrzebne.")]
public void SomeTestUsingExtractedMock()
{
// arrange
int expected = 10;
var dependencyAMock = Mock.Get(_sut.DependencyA);
var dependencyBMock = Mock.Get(_sut.DependencyB);
dependencyAMock.Setup(x => x.QueryMethod(4)).Returns(expected);
// act
var actual = _sut.DoSomething();
// assert
Assert.AreEqual(expected, actual);
dependencyBMock.Verify(x => x.CommandMethod(4));
}
}
public interface IDependencyA
{
int QueryMethod(int q);
}
public interface IDependencyB
{
void CommandMethod(int c);
}
public interface IComponentWithPropertyInjection
{
IDependencyA DependencyA { get; set; }
IDependencyB DependencyB { get; set; }
}
public class ComponentWithPropertyInjection : IComponentWithPropertyInjection
{
public IDependencyA DependencyA { get; set; }
public IDependencyB DependencyB { get; set; }
public int DoSomething()
{
DependencyB.CommandMethod(4);
return DependencyA.QueryMethod(4);
}
}
[TestFixture]
public class ComponentWithPropertyInjectionTests
{
private ComponentWithPropertyInjection _sut;
private IFixture _mockFixture;
private Mock<IDependencyA> _dependencyAMock;
private Mock<IDependencyB> _dependencyBMock;
[SetUp]
public void Setup()
{
_mockFixture = new Fixture().Customize(new AutoMoqCustomization());
_mockFixture.Customize<ComponentWithPropertyInjection>(c => c.FromFactory(new MethodInvoker(new GreedyConstructorQuery())));
_dependencyAMock = _mockFixture.Freeze<Mock<IDependencyA>>();
_dependencyBMock = _mockFixture.Freeze<Mock<IDependencyB>>();
_sut = _mockFixture.Create<ComponentWithPropertyInjection>();
}
[Test]
public void SomeTest()
{
// arrange
int expected = 10;
_dependencyAMock.Setup(x => x.QueryMethod(4)).Returns(expected);
// act
var actual = _sut.DoSomething();
// assert
Assert.AreEqual(expected, actual);
_dependencyBMock.Verify(x => x.CommandMethod(4));
}
[Test(Description = "Z tymi testami freezowanie nie jest potrzebne.")]
public void SomeTestUsingExtractedMock()
{
// arrange
int expected = 10;
var dependencyAMock = Mock.Get(_sut.DependencyA);
var dependencyBMock = Mock.Get(_sut.DependencyB);
dependencyAMock.Setup(x => x.QueryMethod(4)).Returns(expected);
// act
var actual = _sut.DoSomething();
// assert
Assert.AreEqual(expected, actual);
dependencyBMock.Verify(x => x.CommandMethod(4));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment