我不知道如何在 WinRT 应用程序 (metro) 中使用 nUnit。 我编写这段代码并运行测试(使用 Resharper 测试运行器)。 测试通过了。为什么?
using System.Threading.Tasks;
using NUnit.Framework;
namespace UnitTestInWinRT
{
[TestFixture]
public class NUnitClassTest
{
[Test]
public void TestnUnitAsyncTest()
{
var number = GetNumberAsync(7);
number.ContinueWith(n => Assert.AreEqual("string is 6", n.Result));
}
public Task<string> GetNumberAsync(int n)
{
return Task.Run(() => "string is " + n);
}
}
}
最佳答案
问题是:
您使用 ContinueWith方法。
Ceates a continuation that executes asynchronously when the target Task completes.
因此,NUnit 在其他线程中运行您的 lamba,并结束测试方法。 AssertionExcecption
在其他线程中发生,这就是测试通过的原因。
如果您在同一个线程中运行它,测试将按预期失败。
[TestFixture]
public class NUnitClassTest
{
[Test]
public void TestnUnitAsyncTest()
{
var number = GetNumberAsync(7);
number.Wait();
Assert.AreEqual("string is 6", number.Result);
}
public Task<string> GetNumberAsync(int n)
{
return Task.Run(() => "string is " + n);
}
}
关于c# - 如何在 WinRT 应用程序中使用 nUnit 测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12713417/