ho bisogno di scrivere uno unit test per la funzione successiva e ho visto che posso usare [ExpectedException]ExpectedException Assert
questa è la funzione da testare.
public static T FailIfEnumIsNotDefined<T>(this T enumValue, string message = null)
where T:struct
{
var enumType = typeof (T);
if (!enumType.IsEnum)
{
throw new ArgumentOutOfRangeException(string.Format("Type {0} is not an Enum, therefore it cannot be checked if it is Defined not have defined.", enumType.FullName));
}
else if (!Enum.IsDefined(enumType, enumValue))
{
throw new ArgumentOutOfRangeException(string.Format("{1} Value {0} is not does not have defined value in Enum of type {0}. It should not be...", enumType.FullName, message ?? ""));
}
return enumValue;
}
e qui sarebbe andato il codice per testare le eccezioni che dovrebbero essere gettato
[TestMethod]
[ExpectedException(ArgumentOutOfRangeException(ArgumentException), "message")]
public void FailIfEnumIsNotDefined_Check_That_The_Value_Is_Not_Enum()
{
// PREPARE
// EXECUTE
// ASSERT
}
Non ho idea deve fare l'asserzione per le eccezioni sia.
Utilizzare 'Assert.Throws()' invece di 'ExpectedException', in quanto questo attributo supera il test se l'esc eption si è verificato in qualsiasi parte del codice del metodo di test. 'Assert.Throws' consente di verificare la posizione esatta del codice in cui si verifica l'eccezione. – sthotakura
@sthotakura MSTest non ha Assert.Throws a meno che non si scriva uno personalizzato, il che è comunque la gestione dell'eccezione MSTest preferita. Simile a NUnit :) C'è un collegamento nella mia risposta – Spock
@Spock Non lo sapevo, grazie! – sthotakura