Skip to content

Fix S6966 FP: Mock.Raise from Moq should be ignored #9697

Open
@a10r

Description

Description

In an async test method, S6966 is raised for Moq's Mock.Raise method, suggesting to use RaiseAsync instead.

RaiseAsync does not work for normal events and requires a Task-returning event signature instead: https://github.com/moq/moq/pull/1313.

I've seen that this rule has a list of exceptions for specific APIs, so maybe it would make sense to add this case to that list.

Repro steps

Consider this:

public interface IReproInterface
{
	event EventHandler SomethingHappened;
}

public class Repro
{
	[Test]
	public async Task Test()
	{
		await Task.Yield();
		
		var mock = new Mock<IReproInterface>();
		
		mock.Raise(i => i.SomethingHappened += null, new EventArgs()); // "Await RaiseAsync instead"
	}
}

Expected behavior

Should not suggest RaiseAsync.

Actual behavior

Suggests to use RaiseAsync instead of Raise.

Known workarounds

--

Related information

  • C#/VB.NET Plugins version: SonarLint 8.6.0.10679
  • Visual Studio version: 17.12.0
  • MSBuild / dotnet version: dotnet 9.0.100
  • Operating System: Windows 11

Metadata

Assignees

No one assigned

    Labels

    Area: C#C# rules related issues.Type: False PositiveRule IS triggered when it shouldn't be.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions