во время написания тестов для приложения .net 8 и moq 4.20.72 (та же ситуация в .net 9).
Я обнаружил, что во время вызова события VaccumpumtivativationEvent с
Код: Выделить всё
mockCommunicator.Raise(e => e.VaccumPumpActivationEvent += null, 1, true);
< /code>
Все работает нормально, но когда я использую < /p>
await mockCommunicator.RaiseAsync(e => e.VaccumPumpActivationEvent+= null, 1, true);
Код: Выделить всё
Mock.RaiseEventAsync -> (Task)Mock.RaiseEvent(mock, expression, parts, arguments);
Так что здесь есть какая -то разница в. Пример
App Project
public class WorkingClass
{
private readonly ICommunicator _Communicator;
public WorkingClass(ICommunicator kommunikator)
{
this._Communicator = kommunikator;
this._Communicator.VaccumPumpActivationEvent += this.Event_VaccumPumpActivation;
}
private void Event_VaccumPumpActivation(int nummer, bool istAktiviert)
{
Console.WriteLine($"EventFired with {nummer}, and {istAktiviert}");
}
}
public delegate void VaccumPumpActivationDelegate(int nummer, bool istAktiviert);
public interface ICommunicator
{
public event VaccumPumpActivationDelegate VaccumPumpActivationEvent;
}
< /code>
Проект тестирования: < /pbr />
< /code>
using Moq;
namespace ConsoleAppBastelProjekt.UnitTests;
[TestClass]
public sealed class Test1
{
[TestMethod]
public async Task TestMethod1()
{
Mock mockCommunicator = new Mock();
WorkingClass proband = new WorkingClass(mockCommunicator.Object);
await mockCommunicator.RaiseAsync(e => e.VaccumPumpActivationEvent += null, 1, true);
}
}
< /code>
system.nullreexexception
hresult = 0x80004003
message = object rewrest /> StackTrace: < /p>
bei consoleappbastelprojekt.unittests.test1.d__0.movenext () в d: \ git \ oreverserepos \ consoleappbastelprojekt \ consoleappbastelprojekt.unittests \ test1.cs: zeile16. /> < /blockquote>
Подробнее здесь: https://stackoverflow.com/questions/796 ... works-fine