This is a migrated thread and some comments may be shown as answers.

How to force an exception with Throw

1 Answer 571 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Javier
Top achievements
Rank 2
Javier asked on 06 May 2013, 03:29 PM
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?

1 Answer, 1 is accepted

Sort by
0
Kaloyan
Telerik team
answered on 07 May 2013, 04:07 PM
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.
Tags
General Discussions
Asked by
Javier
Top achievements
Rank 2
Answers by
Kaloyan
Telerik team
Share this question
or