我是C#的新手,正在尝试评估Xamarin。我一直在制作一个小型的Web API库,目前进展顺利,但现在我正在尝试测试网络组件,遇到了一些问题。
OS: OS X 10.9.1
Xamarin: 4.2.2 (build 2)
Library: PCL targeting 4.0 (Profile158)
Nunit target: Mono / .NET 4.5
这个类/方法:
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Net.Http;
namespace LibTest
{
public class AsyncTest
{
public async Task<string> DoTest()
{
return await new HttpClient().GetStringAsync( "http://www.reddit.com/.json?limit=1" );
}
}
}
NUnit测试类/用例:
using System;
using NUnit.Framework;
using LibTest;
namespace LibTestTests
{
[TestFixture]
public class LibTestTest
{
[Test]
public async void TempTest()
{
string data = await new AsyncTest().DoTest();
Assert.IsNotNull( data );
Assert.IsNull( data );
}
}
}
我的其他测试似乎都能正常工作(解析等),但是这个测试却直接跳过,表现得好像测试已经通过了,但是我不明白为什么会这样,因为这些断言是相互矛盾的。
这种情况目前是否不支持,还是我的实现方式有误?
如果我使用
ContinueWith()
/Wait()
使其同步,它就能按预期工作,但显然我不希望在我的库中使用重复的实现来通过测试。
async () => {}
时编译和运行它,但是然后我会得到一个错误:Could not load type 'System.Object' from assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'。我使用Nuget安装了
Nitro.AsyncEx,版本为
2.1.3`。 - JoshMono/.NET 4.5
тљЌ№╝Ъ - Josh