2

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.".

  • The code you have is validating that `Exception` throws an exception properly. I'd expect you'd want to test /your/ code, not Microsoft's. In which case the filename would be thrown, as usual, from your class/method. – AlG May 12 '16 at 19:39
  • There's a Console Application that read database, get filename and the URL service, then create an instance and call the method passing the filename as a property of an object. The cause that i need to throw an exception is because the Console Application handle that to log and send e-mail. But this test method that i'm creating is to test just a validation of a file, so, I think that all the process does not metter at this moment. – Gabriel Miranda May 12 '16 at 19:56

1 Answers1

2

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
        }
    }
Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
  • In fact, I have to make sure that this TestMethod throw an exception, then I changed the source from Scott Chamberlain by, inserting the ExpectedException and change the != signal to ==, to validate the Exception type and message returned. Thank you very much Scott. – Gabriel Miranda May 12 '16 at 20:38
  • 1
    I fixed it so it now checks that a exception is always thrown. You just need to put a `Assert.Fail(` at the end of the try block. – Scott Chamberlain May 12 '16 at 20:41
  • I'm new on StackOverflow. Tryint to put the code here but I think it's not permited. Instead of throw new Exception(String.Format("Error on file {0}", FileName)); I have a method to validate the filename ValidateFileName(IncorrectFileName); In this method i have to throw an exception case the file name is invalid. Adding the Assert.Fail really correct it. Thanks again Scott. – Gabriel Miranda May 12 '16 at 20:49
  • Edit your question and put the code there as a update. – Scott Chamberlain May 12 '16 at 20:50
  • Another short change. I put both validation type exception and message in one if because the first throw was ignoring the second validation. Code changed if(e.GetType() != typeof(Exception) && e.Message != String.Format("Error on file {0}", FileName)) throw; – Gabriel Miranda May 12 '16 at 21:47