Skip to content

Instantly share code, notes, and snippets.

@dotnetchris
Created December 13, 2011 18:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dotnetchris/1473259 to your computer and use it in GitHub Desktop.
Save dotnetchris/1473259 to your computer and use it in GitHub Desktop.
Using Machine.Fakes to share testing configuration through composition
[Subject(typeof(ThingOrchestrator))]
public class Given_i_am_a_logged_user_and_view_things : WithSubject<ThingOrchestrator>
{
private static readonly List<People> PeopleRepository = new List<People> {.....};
private static readonly List<Thing> ThingsRepository = new List<Thing> { ...... };
private static readonly List<Thing> ResultThings = new List<Thing>();
private static readonly List<Exception> Exceptions = new List<Exception>();
private Establish context = () =>
{
Configure(x => x.For<IThingService>().Use<ThingService>());
With(new InMemoryLoggingConfiguration(Exceptions));
AutoMapperConfiguration.Start();
The<IRepository<Thing>>()
.WhenToldTo(x => x.AsQueryable())
.Return(ThingsRepository.AsQueryable());
The<IRepository<People>>()
.WhenToldTo(x => x.AsQueryable())
.Return(PeopleRepository.AsQueryable());
};
// act
private Because of = () => ResultThings.AddRange(Subject.GetThings());
//// asserts
private It should_contain_3_things = () => ResultThings.Count.ShouldEqual(3);
private It should_contain_no_errors = () => Exceptions.Count.ShouldEqual(0);
}
public class InMemoryLoggingConfiguration
{
public static List<Exception> Exceptions{ get; set; }
public InMemoryLoggingConfiguration(List<Exception> exceptions) { Exceptions = exceptions; }
OnEstablish context = fakeAccessor =>
{
IWebLogger logger = new MemoryLogger(Exceptions);
fakeAccessor.The<IWebLogger>()
.WhenToldTo(x => x.Write(Param<Exception>.IsAnything))
.Callback<Exception>(logger.Write);
fakeAccessor.The<IWebLogger>()
.WhenToldTo(x => x.Write(Param<string>.IsAnything, Param<object[]>.IsAnything))
.Callback<string,object[]>(logger.Write);
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment