How to force an exception with Throw

2 posts, 0 answers
  1. Javier
    Javier avatar
    10 posts
    Member since:
    Apr 2013

    Posted 06 May 2013 Link to this post

    Hi, I am testing this piece of code:

    public void SaveChanges()
           {
               try
               {
                  //some code here
               }
               catch (MyException dbEValEx)
               {
                   RaiseValidationError(dbEValEx);
               }
               catch (MySecondException dbUpdateException)
               {
                   RaiseUpdateException(dbUpdateException);
               }
           }

    What I would like is to force that exception in some way, right now I have tried this:

    //Class to be tested
    var fooUnitOfWork = Mock.Create<UnitOfWork>(Behavior.CallOriginal);
    //Mock private method
    Mock.NonPublic.Arrange(fooUnitOfWork, "RaiseValidationError", dbEntityValidationException).DoNothing().MustBeCalled();
    //Execute method to test
    fooUnitOfWork.SaveChanges();
    Mock.Arrange(() => fooUnitOfWork.SaveChanges()).Throws(new DbEntityValidationException());
    //Make sure that the RaiseDBValidationError is called
    Mock.Assert(fooUnitOfWork);
    
    
    Any ideas of how to do it?

  2. Kaloyan
    Admin
    Kaloyan avatar
    872 posts

    Posted 07 May 2013 Link to this post

    Hi Javier,

    Thank you for reaching Telerik support.

    To test your exception handling, you could arrange a method inside the "try" block to throw an exception of any kind. To demonstrate this, I added the "DoSomething()" method in the UnitOfWork class:
    public void SaveChanges()
    {
        try
        {
            DoSomething();
        }
        catch (MyException dbEValEx)
        {
            RaiseValidationError(dbEValEx);
        }
        catch (MySecondException dbUpdateException)
        {
            RaiseUpdateException(dbUpdateException);
        }
    }
     
    private void DoSomething()
    {
        // TODO: Implement this method
        throw new NotImplementedException();
    }

    Having the above, now you are able to arrange for the DoSomething method inside your test:
    [TestMethod]
    public void ShouldArrangePrivateFuncToThrowAnException()
    {
        // Arrange
        var fooUnitOfWork = Mock.Create<UnitOfWork>(Behavior.CallOriginal);
     
        Mock.NonPublic.Arrange(fooUnitOfWork, "RaiseValidationError", ArgExpr.IsAny<MyException>()).DoNothing().MustBeCalled();
        Mock.NonPublic.Arrange(fooUnitOfWork, "DoSomething").Throws(new DbEntityValidationException());
     
        // Act
        fooUnitOfWork.SaveChanges();
     
        // Assert. Make sure that the RaiseDBValidationError is called
        Mock.Assert(fooUnitOfWork);
    }
    This will test if the private "RaiseValidationError" method has been actually called with any argument of type "MyException".

    Another thing you can achieve with JustMock is to arrange the entire "SaveChanges" method to throw an exception:
    [TestMethod]
    [ExpectedException(typeof(DbEntityValidationException))]
    public void ShouldArrangeSaveChangesToThrowDbEntityValidationException()
    {
        // Arrange
        var fooUnitOfWork = Mock.Create<UnitOfWork>();
     
        Mock.Arrange(() => fooUnitOfWork.SaveChanges()).Throws(new DbEntityValidationException());
     
        // Act
        fooUnitOfWork.SaveChanges();
    }

    However, if none of the above match your case, please contact us again with further information about the exact behavior you are trying to achieve and I will be happy to help.

    Greetings,
    Kaloyan
    the Telerik team
    Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
  3. DevCraft R3 2016 release webinar banner
Back to Top