jeudi 24 septembre 2015

NUnit test fails when not using lambda expression for assertion

If came across a weird problem while writing some NUnit tests. The tests were more complex, but I broke it down to the following code.

[Test]
public void MyTest()
{
    // Assert.That(test(), Is.True.After(1000, 100)); // Fail
    Assert.That(() => test(), Is.True.After(1000, 100)); // Success
}

static int count = 0;

bool test()
{
    Console.WriteLine(++count);
    if (count == 2)
        return true;
    return false;
}

Why does the test only succeed when I use the lambda expression?

EDIT:

To make it more clear: In the first line it seems that test() only gets executed once while () => test() gets executed multiple times.

Aucun commentaire:

Enregistrer un commentaire