سؤال

I have this simple test method below.

[Test]
        public async Task OneSimpleTest1()
        {
            var eightBall = new EightBall();
            var answer = await eightBall.WillIWin();

            Assert.That(answer, Is.True);
        }

The test class looks like this

public class EightBall
    {
        public Task<bool> WillIWin()
        {
            return new Task<bool>(() => true);
        }
    }

I run the tests in Nunit 2.6.2 using the below command.

nunit-console.exe EightBall.dll /framework:net-4.5

However, the test does not seem to return and hangs forever. Is there a special way to run async tests with Nunit 2.6.2. I thought async was supported using Nunit 2.6.2

هل كانت مفيدة؟

المحلول

return new Task<bool>(() => true); creates a task but does't start it. Better use return Task.Run(()=> true); or return Task.FromResult<bool>(true)

You can also change your code to

public Task<bool> WillIWin()
{
    var task = new Task<bool>(() => true);
    task.Start();
    return task;
}

to make it work

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top