Skip to content

Instantly share code, notes, and snippets.

@andreasohlund
Created September 24, 2010 07:09
Show Gist options
  • Save andreasohlund/594968 to your computer and use it in GitHub Desktop.
Save andreasohlund/594968 to your computer and use it in GitHub Desktop.
using System;
using System.Linq.Expressions;
using NServiceBus;
using Rhino.Mocks;
using Rhino.Mocks.Interfaces;
using NSB = NServiceBus;
namespace NullReference.NServiceBus
{
public static class BusTestExtensions
{
public static void AssertWasPublished<T>(this IBus bus, Expression<Predicate<T>> exp) where T : IMessage
{
if (typeof(T).IsInterface)
{
bus.AssertWasCalled(p => p.Publish(Arg<Action<T>>.Matches(
actionOnInterface => VerifyAction(actionOnInterface, exp)
)));
}
else
{
bus.AssertWasCalled(x => x.Publish(Arg<T[]>
.Matches(p => exp.Compile().Invoke(p[0]))));
}
}
public static void AssertWasPublished<T>(this IBus bus) where T : IMessage
{
bus.AssertWasPublished<T>(x => true);
}
public static void AssertWasNotPublished<T>(this IBus bus) where T : IMessage
{
if (typeof(T).IsInterface)
{
bus.AssertWasNotCalled(p => p.Publish(Arg<Action<T>>.Matches(
actionOnInterface => VerifyAction(actionOnInterface, x => true)
)));
}
else
{
bus.AssertWasNotCalled(s => s.Publish(Arg<IMessage[]>.Matches(
a => true)));
}
}
public static void AssertWasNotSentLocally<T>(this IBus bus) where T : IMessage
{
bus.AssertWasNotCalled(s => s.SendLocal(Arg<IMessage[]>.Matches(
a => true)));
}
public static void AssertWasSent<T>(this IBus bus) where T : IMessage
{
bus.AssertWasCalled(x => x.Send(Arg<IMessage[]>.Matches(p => true)));
}
public static void AssertWasSent<T>(this IBus bus, Expression<Predicate<T>> exp) where T : IMessage
{
bus.AssertWasCalled(x => x.Send(Arg<IMessage[]>
.Matches(p => exp.Compile().Invoke((T)p[0]))));
}
public static void AssertWasSent<T>(this IBus bus, string adress, Expression<Predicate<T>> exp) where T : IMessage
{
bus.AssertWasCalled(x => x.Send(Arg<string>.Matches(s => s == adress), Arg<IMessage[]>
.Matches(p => exp.Compile().Invoke((T)p[0]))));
}
public static void AssertWasSentLocally<T>(this IBus bus, Expression<Predicate<T>> exp, Action<IMethodOptions<object>> options) where T : IMessage
{
bus.AssertWasCalled(x => x.SendLocal(Arg<IMessage[]>
.Matches(p => exp.Compile().Invoke((T)p[0]))), options);
}
public static void AssertWasSentLocally<T>(this IBus bus, Action<IMethodOptions<object>> options) where T : IMessage
{
bus.AssertWasSentLocally<T>(x => true, options);
}
public static void AssertWasSentLocally<T>(this IBus bus, Expression<Predicate<T>> exp) where T : IMessage
{
bus.AssertWasCalled(x => x.SendLocal(Arg<IMessage[]>
.Matches(p => exp.Compile().Invoke((T)p[0]))));
}
public static void AssertWasSentLocally<T>(this IBus bus) where T : IMessage
{
bus.AssertWasSentLocally<T>(x => true);
}
public static void AssertReply<T>(this IBus bus, Expression<Predicate<T>> exp) where T : IMessage
{
bus.AssertWasCalled(x => x.Reply(Arg<IMessage[]>
.Matches(p => exp.Compile().Invoke((T)p[0]))));
}
private static bool VerifyAction<T>(Action<T> a, Expression<Predicate<T>> exp) where T : IMessage
{
NSB.Testing.Test.Initialize();
var test = exp.Compile().Invoke(NSB.Testing.Test.CreateInstance(a));
return test;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment