C# 如何在WinRT应用程序中使用nUnit测试?

C# 如何在WinRT应用程序中使用nUnit测试?,c#,nunit,winrt-async,C#,Nunit,Winrt Async,我不知道如何在WinRT应用程序(metro)中使用nUnit。 我编写此代码并运行测试(使用Resharper测试运行程序)。 考试通过了。为什么? using System.Threading.Tasks; using NUnit.Framework; namespace UnitTestInWinRT { [TestFixture] public class NUnitClassTest { [Test] public void Te

我不知道如何在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);
        }
    }
}
使用System.Threading.Tasks;
使用NUnit.Framework;
名称空间UnitTestInRt
{
[测试夹具]
公共类NUnitClassTest
{
[测试]
public void TestnUnitAsyncTest()
{
变量编号=GetNumberAsync(7);
ContinueWith(n=>Assert.AreEqual(“字符串为6”,n.Result));
}
公共任务GetNumberAsync(int n)
{
返回任务运行(()=>“字符串为”+n);
}
}
}
问题是:

你使用的方法

执行在目标任务完成时异步执行的延续

所以,NUnit在其他线程中运行lamba,并结束测试方法
assertionexception
发生在其他线程中,这就是测试通过的原因

如果在同一线程中运行,测试将按预期失败

[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);
    }
}
[TestFixture]
公共类NUnitClassTest
{
[测试]
public void TestnUnitAsyncTest()
{
变量编号=GetNumberAsync(7);
number.Wait();
aresequal(“字符串是6”,number.Result);
}
公共任务GetNumberAsync(int n)
{
返回任务运行(()=>“字符串为”+n);
}
}