What I want is Assert.ThrowsAsync so I can use it with the new .NET 4.6. However Unity’s NUnit does not have it.
I tried replacing nunit.framework.dll in Unity app folder inside Managed folder, delete Library folder of my game, let it rebuild, reimport all, etc. but still Assert.ThrowsAsync is not available. Any way to replace the shipped library?
I made my own ThrowsAssert, just in case anyone needs it. You can await on it.
public static class E7Assert
{
public static async Task ThrowsAsync<T>(Task asyncMethod) where T : Exception
{
await ThrowsAsync<T>(asyncMethod,"");
}
public static async Task ThrowsAsync<T>(Task asyncMethod,string message) where T : Exception
{
try
{
await asyncMethod; //Should throw..
}
catch(T)
{
//Ok! Swallow the exception.
return;
}
catch(Exception e)
{
if(message != "")
{
Assert.That(e, Is.TypeOf<T>(), message + " " + e.ToString()); //of course this fail because it goes through the first catch..
}
else
{
Assert.That(e, Is.TypeOf<T>(), e.ToString());
}
throw; //probably unreachable
}
Assert.Fail("Expected an exception of type " + typeof(T).FullName + " but no exception was thrown." );
}
}
@EirikWahl Awesome, just what I needed, face the same problems, UniTask would have been the other alternative, but I didn’t want to add one more dependency to my in-house upm package. Thanks!