Guest User

Untitled

a guest
Jun 23rd, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.08 KB | None | 0 0
  1. using System;
  2. using System.Linq.Expressions;
  3. using NServiceBus;
  4. using Rhino.Mocks;
  5. using Rhino.Mocks.Interfaces;
  6. using NSB = NServiceBus;
  7.  
  8. namespace NullReference.NServiceBus
  9. {
  10. public static class BusTestExtensions
  11. {
  12. public static void AssertWasPublished<T>(this IBus bus, Expression<Predicate<T>> exp) where T : IMessage
  13. {
  14. if (typeof(T).IsInterface)
  15. {
  16. bus.AssertWasCalled(p => p.Publish(Arg<Action<T>>.Matches(
  17. actionOnInterface => VerifyAction(actionOnInterface, exp)
  18. )));
  19.  
  20. }
  21. else
  22. {
  23. bus.AssertWasCalled(x => x.Publish(Arg<T[]>
  24. .Matches(p => exp.Compile().Invoke(p[0]))));
  25.  
  26. }
  27. }
  28.  
  29. public static void AssertWasPublished<T>(this IBus bus) where T : IMessage
  30. {
  31. bus.AssertWasPublished<T>(x => true);
  32. }
  33.  
  34. public static void AssertWasNotPublished<T>(this IBus bus) where T : IMessage
  35. {
  36. if (typeof(T).IsInterface)
  37. {
  38. bus.AssertWasNotCalled(p => p.Publish(Arg<Action<T>>.Matches(
  39. actionOnInterface => VerifyAction(actionOnInterface, x => true)
  40. )));
  41. }
  42. else
  43. {
  44. bus.AssertWasNotCalled(s => s.Publish(Arg<IMessage[]>.Matches(
  45. a => true)));
  46.  
  47. }
  48. }
  49.  
  50. public static void AssertWasNotSentLocally<T>(this IBus bus) where T : IMessage
  51. {
  52. bus.AssertWasNotCalled(s => s.SendLocal(Arg<IMessage[]>.Matches(
  53. a => true)));
  54. }
  55.  
  56.  
  57. public static void AssertWasSent<T>(this IBus bus) where T : IMessage
  58. {
  59. bus.AssertWasCalled(x => x.Send(Arg<IMessage[]>.Matches(p => true)));
  60. }
  61.  
  62. public static void AssertWasSent<T>(this IBus bus, Expression<Predicate<T>> exp) where T : IMessage
  63. {
  64. bus.AssertWasCalled(x => x.Send(Arg<IMessage[]>
  65. .Matches(p => exp.Compile().Invoke((T)p[0]))));
  66. }
  67.  
  68. public static void AssertWasSent<T>(this IBus bus, string adress, Expression<Predicate<T>> exp) where T : IMessage
  69. {
  70. bus.AssertWasCalled(x => x.Send(Arg<string>.Matches(s => s == adress), Arg<IMessage[]>
  71. .Matches(p => exp.Compile().Invoke((T)p[0]))));
  72. }
  73.  
  74. public static void AssertWasSentLocally<T>(this IBus bus, Expression<Predicate<T>> exp, Action<IMethodOptions<object>> options) where T : IMessage
  75. {
  76. bus.AssertWasCalled(x => x.SendLocal(Arg<IMessage[]>
  77. .Matches(p => exp.Compile().Invoke((T)p[0]))), options);
  78. }
  79.  
  80. public static void AssertWasSentLocally<T>(this IBus bus, Action<IMethodOptions<object>> options) where T : IMessage
  81. {
  82. bus.AssertWasSentLocally<T>(x => true, options);
  83. }
  84.  
  85. public static void AssertWasSentLocally<T>(this IBus bus, Expression<Predicate<T>> exp) where T : IMessage
  86. {
  87. bus.AssertWasCalled(x => x.SendLocal(Arg<IMessage[]>
  88. .Matches(p => exp.Compile().Invoke((T)p[0]))));
  89. }
  90.  
  91. public static void AssertWasSentLocally<T>(this IBus bus) where T : IMessage
  92. {
  93. bus.AssertWasSentLocally<T>(x => true);
  94. }
  95.  
  96.  
  97. public static void AssertReply<T>(this IBus bus, Expression<Predicate<T>> exp) where T : IMessage
  98. {
  99. bus.AssertWasCalled(x => x.Reply(Arg<IMessage[]>
  100. .Matches(p => exp.Compile().Invoke((T)p[0]))));
  101. }
  102.  
  103.  
  104.  
  105. private static bool VerifyAction<T>(Action<T> a, Expression<Predicate<T>> exp) where T : IMessage
  106. {
  107. NSB.Testing.Test.Initialize();
  108.  
  109. var test = exp.Compile().Invoke(NSB.Testing.Test.CreateInstance(a));
  110.  
  111. return test;
  112. }
  113.  
  114.  
  115. }
  116. }
Add Comment
Please, Sign In to add comment