C# 如何在cancellationTokenSource被取消时进行单元测试

C# 如何在cancellationTokenSource被取消时进行单元测试,c#,unit-testing,cancellationtokensource,C#,Unit Testing,Cancellationtokensource,我想做一个单元测试来检查一个计时器是否取消了cancellationTokenSource 这是我的计时器代码: public class RunTimer { public bool IsTimeLeft { get; private set; } = true; public static void Timer(int numberOfSeconds) { var whenToStop = DateTime.No

我想做一个单元测试来检查一个计时器是否取消了cancellationTokenSource

这是我的计时器代码:

public class RunTimer
    {
        public bool IsTimeLeft { get; private set; } = true;
        public static void Timer(int numberOfSeconds)
        {
            var whenToStop = DateTime.Now.AddSeconds(numberOfSeconds);
            while (DateTime.Now < whenToStop)
            {
                string timeLeft = (whenToStop - DateTime.Now).ToString(@"hh\:mm\:ss");
                WriteToScreen($"Time Remaining: {timeLeft}", true);
                Thread.Sleep(1000);
            }
            if (DateTime.Now >= whenToStop)
            {
                cancellationTokenSource.Cancel();
            }
        }

        static CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
        readonly CancellationToken cancellationToken = cancellationTokenSource.Token;
        public Task timerTask;
        public RunTimer(int numberOfSeconds)
        {
            timerTask = Task.Run(() =>
            {
                while (!cancellationToken.IsCancellationRequested)
                {
                    Timer(numberOfSeconds);
                }
                timerTask = null;
                IsTimeLeft = false;
                cancellationTokenSource.Dispose();
                cancellationTokenSource = new CancellationTokenSource();
            }, cancellationToken);
        }
        public void StopTimer(int numberOfQuestionsLeft)
        {
            if(numberOfQuestionsLeft == 0)
            {
                cancellationTokenSource.Cancel();
            }
        }
    }
这是我尝试过的单元测试代码:

[Test]
        public void IsTimerCancellationRequested()
        {
            CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
            //I set the numberOfQuestions to 0 so the StopTimer method is called and the cts is cancelled
            Program.RunTest(0, UserDifficulty.Easy, 30);
            Assert.That(cancellationTokenSource.IsCancellationRequested)
        }
问题是,我在单元测试中使用的cts与我在定时器方法中使用的cts不同。但我不知道如何修复它


感谢您的帮助

您遇到的第一个问题是,您不应该在同一个测试中测试
RunTest
RunTimer
,否则就不是单元测试。我的建议是分别测试它们,其中包括模拟
RunTimer
,然后在测试
RunTimer
时,我甚至会模拟
DateTime
,因为依赖时间的测试不是很实际。您遇到的第一个问题是,您不应该在同一测试中测试
RunTest
RunTimer
,否则就不是单元测试。我的建议是分别测试它们,包括模拟
RunTimer
,然后在测试
RunTimer
时,我甚至会模拟
DateTime
,因为测试依赖时间的东西不是很实际
[Test]
        public void IsTimerCancellationRequested()
        {
            CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
            //I set the numberOfQuestions to 0 so the StopTimer method is called and the cts is cancelled
            Program.RunTest(0, UserDifficulty.Easy, 30);
            Assert.That(cancellationTokenSource.IsCancellationRequested)
        }