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
using NUnit.Framework;using Moq;using Moq.Protected;namespace MoqAbstractProtected;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 AssignNullToNotNullAttribute
Connection.StartWatchdog();});// Assert
Assert.That(exception, Is.Not.Null);
Assert.That(exception.Message, Is.EqualTo("The Init function must be called first."));}[Test]publicvoidStartWatchdog_ConnectionIsDisposed_WatchdogInternalNotCalled(){// Prepare
Connection.Init();
Connection.Dispose();// Test
Assert.DoesNotThrow(()=>{ Connection.StartWatchdog();});// Assert
ConnectionMock.Protected().Verify(WatchdogInternalAsync, Times.Never());}[Test]publicvoidStartWatchdog_WatchdogInternal_FunctionIsCalled1(){// Prepare
Connection.Init();// Test
Connection.StartWatchdog();// Assert
ConnectionMock.Protected().Verify(WatchdogInternalAsync, Times.Once());}[Test]publicvoidStartWatchdog_WatchdogInternal_FunctionIsCalled2(){// Prepare
Connection.Init();// Test
Connection.StartWatchdog();// Assert
ConnectionMock.Protected().Verify(WatchdogInternalAsync, Times.Once());}[Test]publicvoidStartWatchdog_WatchdogInternal_FunctionIsCalled3(){// Prepare
Connection.Init();// Test
Connection.StartWatchdog();
Connection.StartWatchdog();// Assert
ConnectionMock.Protected().Verify(WatchdogInternalAsync, Times.Exactly(2));}}
namespace MoqAbstractProtected;publicabstractclassAbstractClass:IDisposable{privatereadonlyCancellationTokenSourceCancellationTokenSource;protectedCancellationTokenCancellationToken{get;}protectedboolInitDone{get;privateset;}protectedAbstractClass(){CancellationTokenSource=new CancellationTokenSource();CancellationToken= CancellationTokenSource.Token;}publicvoidInit(){
Console.Out.WriteLine("Init");if(InitDone){thrownew Exception("Init already called. Dispose this instance and create a new one.");}InitDone=true;}publicvoidStartWatchdog(){if(InitDone isfalse){thrownew Exception($"The {nameof(Init)} function must be called first.");}
Task.Factory.StartNew(WatchdogAsync, CancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Default);}privateasync Task WatchdogAsync(){if(CancellationToken.IsCancellationRequested){return;}await WatchdogInternalAsync();// This is a blocking call
Console.Out.WriteLine("Watchdog has stopped monitoring. Wuff!");}protectedabstract Task WatchdogInternalAsync();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: