locked
Method throws exception test RRS feed

  • Question

  • Hi all, 

    I wanted to test below class

    public class SaveSQL: ISaveSQL
        {
         
            private readonly IErrorLog _errorLog;
            public SaveSQL(IErrorLog errorLog) 
            {
                _errorLog = errorLog;
            }

            public void Save(string)
            {
                try
                {

                    //

                }
                catch (Exception exception)
                {
                    _errorLog.LogError("anything");
                    throw;
                }
            }

    private void ExecuteSQL()
    {
       /Execute SQL here

    }
          

        }

    and i wanted to write a nNuit to test my Save  method ,   and how do we test my method to make sure that it log errors if something goes wrong with my method ? what is the best practice in here 

    thanks  

    Wednesday, April 10, 2019 10:48 AM

All replies

  • Hello,

    A pattern to consider is as follows, the test will pass when the DivideByZeroException is thrown. So in your case replace DivideByZeroException with the exception you expected which if unsure then run the test method under the debugger and examine what the error is.

    [TestMethod()]
    [ExpectedException(typeof(System.DivideByZeroException))]
    public void DivideTest()
    {
    	int numerator = 4;
    	int denominator = 0;
    	int actual = numerator / denominator;
    }
    See also ExpectedException


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, April 10, 2019 12:02 PM
  • hi , 

    ExpectedException Attribute has been removed from the latest version of nUnit  as it was a bad practice . 

    Please see this link below :

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

    are ther any other way to test this ? I am unisng nSubstitute as my mocking framework .

    thanks

    Wednesday, April 10, 2019 12:21 PM
  • Yes you can test it without using the attribute. I personally recommend you use FluentAssertions or equivalent which makes this kind of test very easy.

    Action action = () => //Do work that should throw an exception
    
    action.Should().Throw<SomeException>();

    You can expand on that as needed. If the action doesn't throw then the test will fail. In your specific case where you are expecting it to have logged an exception then that will be a separate check.

    //I have no idea what you're IErrorLog is so we'll assume List<string>
    var mockLog = new List<string>();
    
    Action action = () => //Do test
    
    action.Should().Throw<Exception>();
    mockLog.Should().HaveCount(1);
    If you don't want to use a third party library then you'd need to write the equivalent code. Basically Throw<T> executes the action in a try-catch and catches the exception. Then it compares it to what is expected and fails the call if it doesn't line up. You can write all this code yourself but using a third party library is cleaner and more efficient on your time.


    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, April 10, 2019 1:58 PM
  • Hi

    Thank you for posting here.

    Since this thread is related to visual studio unit Testing, I will move it to visual studio unit Testing forum for suitable support.

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=vsunittest

    The Visual C# forum discuss and ask questions about the C# programming language, IDE, libraries, samples, and tools.

    Best regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, April 11, 2019 7:11 AM
  • Hi Kavithma_su,

    It seems that you have solved this issue.

    If the reply is helpful, please mark it as answer, it will be beneficial to other community members which have the similar issue.

    Regards,

    Dylan


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com

    Thursday, April 25, 2019 10:19 AM