Skip to content

Commit 1cd8aeb

Browse files
committed
Add WithMessageBus to ReactiveUI.Testing
1 parent 152f488 commit 1cd8aeb

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

ReactiveUI.Testing/TestUtils.cs

+30
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,37 @@ public static void With(this TestScheduler sched, Action<TestScheduler> block)
9090
sched.With(x => { block(x); return 0; });
9191
}
9292

93+
/// <summary>
94+
/// WithMessageBus allows you to override the default Message Bus
95+
/// implementation until the object returned is disposed. If a
96+
/// message bus is not specified, a default empty one is created.
97+
/// </summary>
98+
/// <param name="messageBus">The message bus to use, or null to create
99+
/// a new one using the default implementation.</param>
100+
/// <returns>An object that when disposed, restores the original
101+
/// message bus.</returns>
102+
public static IDisposable WithMessageBus(this TestScheduler sched, IMessageBus messageBus = null)
103+
{
104+
var origMessageBus = RxApp.MessageBus;
105+
106+
RxApp.MessageBus = messageBus ?? new MessageBus();
107+
return Disposable.Create(() => RxApp.MessageBus = origMessageBus);
108+
}
93109

110+
/// <summary>
111+
/// WithMessageBus allows you to override the default Message Bus
112+
/// implementation for a specified action. If a message bus is not
113+
/// specified, a default empty one is created.
114+
/// <param name="block">The action to execute.</param>
115+
/// <param name="messageBus">The message bus to use, or null to create
116+
/// a new one using the default implementation.</param>
117+
public static void WithMessageBus(this TestScheduler sched, Action<IMessageBus> block, IMessageBus messageBus = null)
118+
{
119+
messageBus = messageBus ?? new MessageBus();
120+
using(var _ = sched.WithMessageBus(messageBus)) {
121+
block(messageBus);
122+
}
123+
}
94124

95125
/// <summary>
96126
/// RunToMilliseconds moves the TestScheduler to the specified time in

ReactiveUI/RxApp.cs

+15-1
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,25 @@ public static IScheduler TaskpoolScheduler {
140140
/// </summary>
141141
public static Func<string, ILog> LoggerFactory { get; set; }
142142

143+
144+
[ThreadStatic] static IMessageBus _UnitTestMessageBus;
145+
static IMessageBus _MessageBus;
146+
143147
/// <summary>
144148
/// Set this property to implement a custom MessageBus for
145149
/// MessageBus.Current.
146150
/// </summary>
147-
public static IMessageBus MessageBus { get; set; }
151+
public static IMessageBus MessageBus {
152+
get { return _UnitTestMessageBus ?? _MessageBus; }
153+
set {
154+
if (InUnitTestRunner()) {
155+
_UnitTestMessageBus = value;
156+
_MessageBus = _MessageBus ?? value;
157+
} else {
158+
_MessageBus = value;
159+
}
160+
}
161+
}
148162

149163
#if FALSE
150164
public static Func<UserException, RecoveryOptionResult> CustomErrorPresentationFunc { get; set; }

0 commit comments

Comments
 (0)