Domanda

I am trying to write unit test for my async method that throws exception.

Here is my unit test. I am expecting WebException. The test fails as WebException is not returned immediately. Test runner exits with Unhandled Exception.

Is there any way of testing exceptions in async methods with Touch.Unit?

[Test]
[ExpectedException(typeof(WebException))]
public async void Throws_Exception_When_GetOrders()
{
    DataService service = new DataService();  
    await service.GetOrders();
}

Console output

2013-07-01 16:46:08.552 TestsiOS[32724:c07] ExceptionsTests
2013-07-01 16:46:09.855 TestsiOS[32724:c07]     [FAIL] Throws_Exception_When_GetOrders : System.Net.WebException was expected

...

Unhandled Exception:
System.Net.WebException: Error: NameResolutionFailure
  at System.Net.HttpWebRequest.EndGetResponse (IAsyncResult asyncResult) [0x00065] in /Developer/MonoTouch/Source/mono/mcs/class/System/System.Net/HttpWebRequest.cs:947 
  at RestSharp.Http.GetRawResponseAsync (IAsyncResult result, System.Action`1 callback) [0x00000] in <filename unknown>:0 
...

I have tried to return Task in my test method. It simply ignores my test method.

public async Task Throws_Exception_When_GetOrders()

I have also tried this. And the test run hangs up.

public void Throws_Exception_When_GetOrders()
{
    DataService service = new DataService();  
    service.GetOrders().Wait();
}
È stato utile?

Soluzione

Xamarin.iOs 6.3 has updated Touch.Unit and it now supports testing async methods. https://bugzilla.xamarin.com/show_bug.cgi?id=14312

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top