Skip to content

Instantly share code, notes, and snippets.

@ewilde
Last active December 11, 2015 17:28
Show Gist options
  • Save ewilde/4634814 to your computer and use it in GitHub Desktop.
Save ewilde/4634814 to your computer and use it in GitHub Desktop.
#mspec base class which creates a subject with DI instead of mocks, useful for integration test
public class When_calling_foo_service : WithIntegrationSubject<ServiceClientWrapper<IFooService>>
{
Establish context = () => With<RunningService>();
Because of = () => FooActionResponse = Subject.Channel.Action(new FooActionRequest());
It should_return_a_response = () => FooActionResponse.ShouldNotBeNull();
static FooActionResponse FooActionResponse;
}
public class RunningService
{
static bool initialized;
OnEstablishIntegration context = () =>
{
if (!initialized)
{
FooServerEngine.Start(new);
AppDomain.CurrentDomain.DomainUnload += (sender, args) => FooServerEngine.Stop();
initialized = true;
}
};
}
using System;
using System.Collections.Generic;
using System.Linq;
using Machine.Fakes;
using Machine.Fakes.Sdk;
using Machine.Specifications;
using Machine.Specifications.Runner.Impl;
using StructureMap;
public class WithIntegrationSubject<TSubject> : WithIntegrationSubject where TSubject : class, new()
{
private static TSubject subject;
private static List<object> withs;
static WithIntegrationSubject()
{
withs = new List<object>();
}
public static TSubject Subject
{
get
{
return subject ?? (subject = ObjectFactory.GetInstance<TSubject>());
}
}
public static T With<T>() where T : new()
{
var with = new T();
withs.Add(with);
with.GetFieldValues<OnEstablishIntegration>()
.ForEach<OnEstablishIntegration>(establishDelegate => establishDelegate.Invoke());
return with;
}
public static T Then<T>()
{
return (T)withs.First(item => item.GetType() == typeof(T));
}
Cleanup cleanup =
() =>
withs.ForEach(
with =>
with.GetFieldValues<OnCleanup>()
.ForEach<OnCleanup>(establishDelegate => establishDelegate.Invoke(Subject)));
}
public class WithIntegrationSubject
{
static WithIntegrationSubject()
{
ObjectFactory.Configure(x =>
x.Scan(scanner =>
{
scanner.AssembliesFromApplicationBaseDirectory(
filter => filter.FullName.Contains("RADS"));
scanner.WithDefaultConventions();
}));
}
}
public delegate void OnEstablishIntegration();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment