none
Boolean Pop() method with exception to check in MSTest

    Question

  • So im using MSTest and learning about it. My Problem is, that i am trying to do is give a exception. When the exception is thrown the application should Close. But then i cant test my program with a Boolean Method. Is there a way around this?

    public bool Pop()
            {             
                    Node temp = Head;
    
                    if (CountPosition > 0)
                    {
                        CountPosition--;
                        temp.Data = null;
                        Head = temp.Next;
    
                        //return true;
                    }            
                if (CountPosition == 0)
                {
                    
                    if (temp == null)
                    {
                         
                        throw new IOException();
                               
                    }              
                    
                }  
                //return false;
       
                  
            }

    and here is my simple exception method

    public class IOException : Exception   
        {       
            public IOException()
            {            
                
               
            }
            public void ErrorMessage()
            {
                Console.WriteLine("Pop() was used in a Empty stack");
                Console.WriteLine("Please Try Again");
                Console.WriteLine("Closing the Program");            
                Console.ReadLine();
    
            }
        }


    • Edited by k9nneo1191 Thursday, March 30, 2017 7:59 AM
    Thursday, March 30, 2017 7:55 AM

All replies

  • here is my test method by the way

    [TestMethod]
            public void BasicPop_1_Input()
            {         
                             
                  
                StackLinkedList.List MyList = new StackLinkedList.List();
                            
                 bool test = MyList.Pop();
                
    
    
                Assert.AreEqual(false,test);
    
            }


    • Edited by k9nneo1191 Thursday, March 30, 2017 7:58 AM
    Thursday, March 30, 2017 7:58 AM
  • For code that throws an exception you should test that the exception is thrown in your test.

    [TestMethod]
    [ExpectedException(typeof(IOException)]
    public void BasicPop_1_Input()
    {
       var myList = new StackedLinkedList.List();
       
       //Expect exception here
       MyList.Pop();
    }

    Unfortunately MSTest doesn't allow you better grain control over the exception. For example, even though you're testing Pop if List() for some reason throws the exception your test still passes.

    Personally I use FluentAssertions (in combination with MSTest) to do my testing but other testing frameworks and libraries support doing a more focused exception handling. As an example here's how I'd do it using FA.

    [TestMethod]
    public void BasicPop_1_Input()
    {
       var myList = new StackedLinkedList.List();
    
       Action a => () => myList.Pop();
    
       a.ShouldThrow<IOException>();
    }

    Now this test will only pass if Pop throws the exception I expect.

    Michael Taylor
    http://www.michaeltaylorp3.net

    Thursday, March 30, 2017 2:13 PM
    Moderator