如何使用xUnit Assert.RaisesAny()?我似乎找不到任何示例。
我收到以下语法错误
“ IMqttServer.Started”事件只能出现在左侧 的+ =或-=
这很有意义,因为我没有订阅它,但是我不知道RaisesAny()使用什么语法
基本上,我只是在尝试检查代理是否已启动,至少有一个与其连接的客户端以及它是否已停止。
P.S。 MqttServer是Mqtt Broker实现,它是MqttNet
的一部分这是我的测试
public class ResearchTestingOnly
{
private readonly ITestOutputHelper output;
public ResearchTestingOnly(ITestOutputHelper output)
{
this.output = output;
}
[Fact]
public void Test1()
{
IMqttServer _mqttBroker = new MqttFactory().CreateMqttServer();
var receivedEvents = new List<string>();
_mqttBroker.ClientConnected += delegate (object sender, MqttClientConnectedEventArgs args)
{
receivedEvents.Add(args.ClientId);
};
Assert.RaisesAny<EventHandler>(_mqttBroker.Started);
Assert.RaisesAny<MqttClientConnectedEventArgs>(_mqttBroker.ClientConnected);
Assert.RaisesAny<EventHandler>(_mqttBroker.Stopped);
//** Start Broker
Task _brokerTask = Task.Run(() => _mqttBroker.StartAsync(new MqttServerOptions()));
//** Wait 10 Seconds
var pause = new ManualResetEvent(false);
pause.WaitOne(10000);
//** Stop Broker
Task _brokerStopTask = Task.Run(() => _mqttBroker.StopAsync());
//** Wait for Broker Tasks to Complete
Task.WaitAll(_brokerTask, _brokerStopTask);
output.WriteLine($"Number of Clients Connected: {receivedEvents.Count}");
foreach(var b in receivedEvents)
{
output.WriteLine(b);
}
}
}
答案 0 :(得分:1)
基于the source for EventAssertsTests(将x
替换为_mqttBroker
):
[Fact]
public static void GotExpectedEvent()
{
var evt = Assert.RaisesAnyAsync<EventArgs>(
h => _mqttBroker.Started += h,
h => _mqttBroker.Started -= h,
() => Task.Run(() => _mqttBroker.StartAsync(new MqttServerOptions())));
Assert.NotNull(evt);
Assert.Equal(_mqttBroker, evt.Sender);
Assert.Equal(EventArgs.Empty, evt.Arguments);
}