You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I want to use Mock.Protected().Verify to check that my function (WatchdogInternalAsync) has been executed.
If I run the unit test (StartWatchdog_WatchdogInternal_FunctionIsCalled1) individually, the test is successful.
However, if another test runs before it, the test fails.
If the same test (StartWatchdog_WatchdogInternal_FunctionIsCalled2) is run again afterward, it only fails sometimes.
And if I call my function twice (in StartWatchdog_WatchdogInternal_FunctionIsCalled3) and check for Times.Exactly(2), then this test is always successful.
I have the feeling it is a timing problem.
When I change the names of the test functions, causing them to run in a different order, I sometimes get other tests failing.
Steps to Reproduce
Run all tests
Expected Behavior
All unit tests should be successful
Exception with Stack Trace
Moq.MockException :
Expected invocation on the mock once, but was 0 times: mock => mock.WatchdogInternalAsync()
Performed invocations:
Mock<AbstractGrpcClientConnectionWithWatchdog<ApiService.ApiServiceClient>:3> (mock):
AbstractGrpcClientConnectionWithWatchdog<ApiService.ApiServiceClient>.WatchdogInternalAsync()
#nullable disable
usingNUnit.Framework;usingMoq;usingMoq.Protected;namespaceMoqAbstractProtected;publicclassTests{privateconststringWatchdogInternalAsync="WatchdogInternalAsync";privateMock<AbstractClass>ConnectionMock;privateAbstractClassConnection;[SetUp]publicvoidSetup(){ConnectionMock=newMock<AbstractClass>();ConnectionMock.CallBase=true;Connection=ConnectionMock.Object;ConnectionMock.Protected().Setup<Task>(WatchdogInternalAsync).Callback(()=>Console.Out.WriteLine($"\"{WatchdogInternalAsync}\" was called!")).Returns(Task.CompletedTask);}[Test]publicvoidStartWatchdog_InitNotCalled_ThrowsException(){// Prepare// Connection.Init(); NO init call// TestExceptionexception=Assert.Throws<Exception>(delegate{// ReSharper disable once AssignNullToNotNullAttributeConnection.StartWatchdog();});// AssertAssert.That(exception,Is.Not.Null);Assert.That(exception.Message,Is.EqualTo("The Init function must be called first."));}[Test]publicvoidStartWatchdog_ConnectionIsDisposed_WatchdogInternalNotCalled(){// PrepareConnection.Init();Connection.Dispose();// TestAssert.DoesNotThrow(()=>{Connection.StartWatchdog();});// AssertConnectionMock.Protected().Verify(WatchdogInternalAsync,Times.Never());}[Test]publicvoidStartWatchdog_WatchdogInternal_FunctionIsCalled1(){// PrepareConnection.Init();// TestConnection.StartWatchdog();// AssertConnectionMock.Protected().Verify(WatchdogInternalAsync,Times.Once());}[Test]publicvoidStartWatchdog_WatchdogInternal_FunctionIsCalled2(){// PrepareConnection.Init();// TestConnection.StartWatchdog();// AssertConnectionMock.Protected().Verify(WatchdogInternalAsync,Times.Once());}[Test]publicvoidStartWatchdog_WatchdogInternal_FunctionIsCalled3(){// PrepareConnection.Init();// TestConnection.StartWatchdog();Connection.StartWatchdog();// AssertConnectionMock.Protected().Verify(WatchdogInternalAsync,Times.Exactly(2));}}
namespaceMoqAbstractProtected;publicabstractclassAbstractClass:IDisposable{privatereadonlyCancellationTokenSourceCancellationTokenSource;protectedCancellationTokenCancellationToken{get;}protectedboolInitDone{get;privateset;}protectedAbstractClass(){CancellationTokenSource=newCancellationTokenSource();CancellationToken=CancellationTokenSource.Token;}publicvoidInit(){Console.Out.WriteLine("Init");if(InitDone){thrownewException("Init already called. Dispose this instance and create a new one.");}InitDone=true;}publicvoidStartWatchdog(){if(InitDoneisfalse){thrownewException($"The {nameof(Init)} function must be called first.");}Task.Factory.StartNew(WatchdogAsync,CancellationToken,TaskCreationOptions.LongRunning,TaskScheduler.Default);}privateasyncTaskWatchdogAsync(){if(CancellationToken.IsCancellationRequested){return;}awaitWatchdogInternalAsync();// This is a blocking callConsole.Out.WriteLine("Watchdog has stopped monitoring. Wuff!");}protectedabstractTaskWatchdogInternalAsync();protectedvirtualvoidDispose(booldisposing){Console.Out.WriteLine($"Dispose({disposing})");if(disposing){CancellationTokenSource.Cancel();CancellationTokenSource.Dispose();}}publicvoidDispose(){Dispose(true);Console.Out.WriteLine("Dispose");GC.SuppressFinalize(this);}}
The text was updated successfully, but these errors were encountered:
Describe the Bug
I want to use
Mock.Protected().Verify
to check that my function (WatchdogInternalAsync
) has been executed.If I run the unit test (
StartWatchdog_WatchdogInternal_FunctionIsCalled1
) individually, the test is successful.However, if another test runs before it, the test fails.
If the same test (
StartWatchdog_WatchdogInternal_FunctionIsCalled2
) is run again afterward, it only fails sometimes.And if I call my function twice (in
StartWatchdog_WatchdogInternal_FunctionIsCalled3
) and check forTimes.Exactly(2)
, then this test is always successful.I have the feeling it is a timing problem.
When I change the names of the test functions, causing them to run in a different order, I sometimes get other tests failing.
Steps to Reproduce
Expected Behavior
All unit tests should be successful
Exception with Stack Trace
Version Info
Code
Project:
MoqAbstractProtected.zip
C# Code
The text was updated successfully, but these errors were encountered: