简体   繁体   中英

Async test hangs in Nunit 2.6.2

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM