Last active
December 4, 2015 09:11
-
-
Save sshushliapin/f692eddb1db277817545 to your computer and use it in GitHub Desktop.
AutoFixture-based version of the Greeter class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace AutoFixtureSample | |
{ | |
using System; | |
using NSubstitute; | |
using Ploeh.AutoFixture; | |
using Ploeh.AutoFixture.AutoNSubstitute; | |
using Ploeh.AutoFixture.Xunit2; | |
using Xunit; | |
/// <summary> | |
/// That is the AutoFixture-based version of the Greeter class described by Dan Solovay here: | |
/// http://www.dansolovay.com/2015/11/autofac-aggregate-service.html?showComment=1447605902217 | |
/// Requires the following NuGetPackages to be installed: | |
/// Install-Package xunit | |
/// Install-Package AutoFixture.AutoNSubstitute | |
/// Install-Package AutoFixture.Xunit2 | |
/// </summary> | |
public class GreeterTest | |
{ | |
[Theory, DefaultAutoData] | |
public void Greeter_Hour0_WritesGoodMorning([Frozen]IDateTime fakeTime, [Frozen]IWriter writer, Greeter sut) | |
{ | |
fakeTime.Now.Returns(new DateTime(1, 1, 1, 0, 0, 0)); | |
sut.WriteDate(); | |
writer.Received().WriteLine("Good morning"); | |
} | |
[Theory, DefaultAutoData] | |
public void Greeter_Hour13_WritesGoodAfternoon([Frozen]IDateTime fakeTime, [Frozen]IWriter writer, Greeter sut) | |
{ | |
fakeTime.Now.Returns(new DateTime(1, 1, 1, 13, 0, 0)); | |
sut.WriteDate(); | |
writer.Received().WriteLine("Good afternoon"); | |
} | |
} | |
internal class DefaultAutoDataAttribute : AutoDataAttribute | |
{ | |
public DefaultAutoDataAttribute() | |
: base(new Fixture().Customize(new AutoNSubstituteCustomization())) | |
{ | |
} | |
} | |
public class Greeter | |
{ | |
private readonly IDateTime _dateTime; | |
private readonly IWriter _writer; | |
public Greeter(IDateTime dateTime, IWriter writer) | |
{ | |
_dateTime = dateTime; | |
_writer = writer; | |
} | |
public void WriteDate() | |
{ | |
if (_dateTime.Now.Hour >= 13) | |
{ | |
_writer.WriteLine("Good afternoon"); | |
} | |
else | |
{ | |
_writer.WriteLine("Good morning"); | |
} | |
} | |
} | |
public interface IDateTime | |
{ | |
DateTime Now { get; set; } | |
} | |
public interface IWriter | |
{ | |
void WriteLine(string text); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment