2014-11-19 12 views
15

Sto provando a eseguire un test su NUnit3 e sto ottenendo un System.ArgumentException: i metodi 'async void' non sono supportati, per favore usa invece 'async Task'.NUnit3: Assert.Throws con async Compito

[Test] 
public void InvalidUsername() 
{ 
... 
var exception = Assert.Throws<HttpResponseException>(async() => await client.LoginAsync("[email protected]", testpassword)); 
exception.HttpResponseMessage.StatusCode.ShouldEqual(HttpStatusCode.BadRequest); // according to http://tools.ietf.org/html/rfc6749#section-5.2 
... 
} 

Assert.Throws sembra prendere una TestDelegate, definita come:

public delegate void TestDelegate(); 

quindi l'ArgumentException. Qual è il modo migliore per effettuare il porting di questo codice?

+1

Correlati: http://stackoverflow.com/questions/15634542/nunit-async-test-exception-assertion –

risposta

-2

Si potrebbe provare a utilizzare qualcosa di simile:

try 
{ 
    await client.LoginAsync("[email protected]", testpassword); 
} 
catch (Exception ex) 
{ 
    Assert.That(ex, Is.InstanceOf(typeof (HttpResponseException))); 
} 
1

Per garantire l'eccezione è stato gettato, è meglio di non valere nel blocco catch se così sceglie di utilizzare uno. In questo modo, puoi essere sicuro che venga generato il tipo di eccezione corretto perché altrimenti otterrai un riferimento null o un'eccezione diversa non rilevata.

HttpResponseException expectedException = null; 
try 
{ 
    await client.LoginAsync("[email protected]", testpassword));   
} 
catch (HttpResponseException ex) 
{ 
    expectedException = ex; 
} 

Assert.AreEqual(HttpStatusCode.NoContent, expectedException.Response.BadRequest); 
0

Ho finito per scrivere una funzione statica che rispecchia ciò che NUnit fa. C'è stata un'intera conversazione allo https://github.com/nunit/nunit/issues/464 a riguardo.

public static async Task<T> Throws<T>(Func<Task> code) where T : Exception 
{ 
    var actual = default(T); 

    try 
    { 
     await code(); 
     Assert.Fail($"Expected exception of type: {typeof (T)}"); 
    } 
    catch (T rex) 
    { 
     actual = rex; 
    } 
    catch (Exception ex) 
    { 
     Assert.Fail($"Expected exception of type: {typeof(T)} but was {ex.GetType()} instead"); 
    } 

    return actual; 
} 

Poi dalle mie prove posso usarlo come

var ex = await CustomAsserts.Throws<HttpResponseException>(async() => await client.DoThings()); 
Assert.IsTrue(ex.Response.StatusCode == HttpStatusCode.BadRequest); 
23

Questo è stato risolto da NUnit. È ora possibile utilizzare Assert.ThrowsAsync <>()

https://github.com/nunit/nunit/issues/1190

Esempio:

Assert.ThrowsAsync<Exception>(() => YourAsyncMethod()); 
+1

Puoi fornire un esempio di come usa il metodo 'ThrowsAsync'? – simonlchilds

+1

Modificato il post e fornito un esempio. – Zabbu

1

mi sento di raccomandare il seguente codice al posto di Assert.ThrowsAsync, in quanto questo è più leggibile:

// Act 
AsyncTestDelegate act = async() => await YourAsyncMethod(); 

// Assert 
Assert.That(act, Throws.TypeOf<YourException>()); 
Problemi correlati