+= null syntax when raising events with Raise()/RaiseAsync()| Property | Value |
|---|---|
| Rule ID | PosInfoMoq1010 |
| Title | Use += null syntax when raising events with Raise()/RaiseAsync() |
| Category | Design |
| Default severity | Warning |
When using Mock<T>.Raise() or Mock<T>.RaiseAsync(), the lambda expression used to identify the event should consistently use the += null syntax.
While Moq is flexible and can identify the event even with += SomeMethod, using += null is the conventional and clearest way to indicate that you are merely referencing the event for Moq’s internal use, not actually subscribing a handler. This improves code readability and intent.
public class Service
{
public event EventHandler Changed;
public void DoSomething()
{
this.Changed?.Invoke(this, EventArgs.Empty);
}
}
+= null)var serviceMock = new Mock<Service>();
// Clear intent: referencing the event for Moq
serviceMock.Raise(s => s.Changed += null, EventArgs.Empty);
+= SomeMethod)public class MyTests
{
private void MyEventHandler(object sender, EventArgs e) { /* ... */ }
[Fact]
public void Test()
{
var serviceMock = new Mock<Service>();
// While functional, less clear intent
serviceMock.Raise(s => s.Changed += MyEventHandler, EventArgs.Empty); // ⚠️
}
}
Change the lambda expression to use += null when identifying the event for Raise() or RaiseAsync().
Correct:
serviceMock.Raise(s => s.Changed += null, EventArgs.Empty);
Incorrect:
serviceMock.Raise(s => s.Changed += MyEventHandler, EventArgs.Empty);
This is a design guideline for code readability. While suppressing it won’t cause runtime errors, it’s generally recommended to follow for consistent and clear code.