-
-
Save haf/00928ee8e6eba7817aa9 to your computer and use it in GitHub Desktop.
This file contains 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
using Castle.Core; | |
using Castle.Core.Internal; | |
using Castle.MicroKernel; | |
using Castle.MicroKernel.Facilities; | |
using Castle.MicroKernel.Registration; | |
using Castle.Windsor; | |
using System; | |
using System.Collections.Generic; | |
using System.Threading; | |
using Castle.DynamicProxy; | |
namespace Prover.SpecWriter { | |
public static class WindsorContainerFactory { | |
public static IWindsorContainer GetTracingContainer(params IRegistration[] registrations) { | |
var container = new WindsorContainer(); | |
container.AddFacility<SpecWriterFacility>(); | |
if (registrations != null) container.Register(registrations); | |
return container; | |
} | |
} | |
/// <summary> | |
/// Bootstrapper for the integration test tracing infrastructure | |
/// </summary> | |
public class SpecWriterFacility | |
: AbstractFacility { | |
public const string InterceptorKey = "tracert-interceptor"; | |
protected override void Init() { | |
Kernel.Register(Component.For<RecordingInterceptor>().Named(InterceptorKey) | |
.LifeStyle.Singleton); | |
Kernel.ComponentRegistered += Kernel_ComponentRegistered; | |
} | |
void Kernel_ComponentRegistered(string key, IHandler handler) { | |
if (handler.ComponentModel.Implementation.HasAttribute<RecordInteractionsAttribute>()) { | |
handler.ComponentModel.Interceptors.Add(new InterceptorReference(InterceptorKey)); | |
} | |
} | |
} | |
public class RecordingInterceptor | |
: IInterceptor { | |
void IInterceptor.Intercept(IInvocation invocation) { | |
invocation.Proceed(); | |
} | |
} | |
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)] | |
public class RecordInteractionsAttribute | |
: Attribute { | |
} | |
} |
This file contains 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
PS C:\Users\haf\Documents\Visual Studio 2012\Projects\MSpecSample> .\packages\Machine.Specifications.0.5.10\tools\mspec-clr4.exe .\bin\Debug\MSpecSample.dll | |
Specs in MSpecSample: | |
When getting random number | |
» should return less than ten | |
Contexts: 1, Specifications: 1, Time: 0.14 seconds |
This file contains 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
container.Register(Component.For<MyServiceInterface>().ImplementedBy<MyClass>()); | |
// using code above: | |
WindsorContainerFactory.GetTracingContainer(Component.For<MyServiceInterface>().ImplementedBy<MyClass>()); | |
// using MSpec | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using Machine.Specifications; | |
namespace MSpecSample | |
{ | |
public class FiveFactory | |
{ | |
public static int GetRandomNumber() | |
{ | |
return 5; | |
} | |
} | |
public class When_getting_random_number | |
{ | |
It should_return_less_than_ten = | |
() => FiveFactory.GetRandomNumber() | |
.ShouldBeLessThan(10); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment