I'm trying to validate the exception and message returned, but i have a file name in this message that is variable. Is possible to do that using unit test in just one method?
public static string FileName
{
get
{
return "EXT_RF_ITAUVEST_201605091121212";
}
}
[TestMethod()]
[ExpectedException(typeof(Exception), String.Format("Error on file {0}", FileName))]
public void ValidarNomeArquivo_DataNomeIncorreta_Mensagem()
{
throw new Exception(String.Format("Error on file {0}", FileName));
}
The code above return the error "An attribute argument must be constant expression, typeof expression or array creation expression of an attribute parameter type.".
In your situation I would not use ExpectedException
and instead just manually do the logic it does.
public static string FileName
{
get
{
return "EXT_RF_ITAUVEST_201605091121212";
}
}
[TestMethod()]
public void ValidarNomeArquivo_DataNomeIncorreta_Mensagem()
{
//This try block must contain the entire function's logic,
// nothing can go after it to get the same behavor as ExpectedException.
try
{
throw new Exception(String.Format("Error on file {0}", FileName));
//This line must be the last line of the try block.
Assert.Fail("No exception thrown");
}
catch(Exception e)
{
//This is the "AllowDerivedTypes=false" check. If you had done AllowDerivedTypes=true you can delete this check.
if(e.GetType() != typeof(Exception))
throw;
if(e.Message != String.Format("Error on file {0}", FileName))
throw;
//Do nothing here
}
}