After reaching the expected test result

I am testing a method to remove an abstract base repository that is inherited by several other repository classes. MyTestRepository inherits the base, so I can run tests against the base methods without restricting my test to use a particular class. When I run my unit test, it passes, but then I noticed that I have several OrderDetail and Schedule objects in the test database that were generated by the test (objects are created during test initialization) and are not deleted, and the Order object is deleted. I added some breakpoints and noticed that as soon as the helper method ends and the expected exception was thrown, the test ends and other calls to the helper never occur.

This is my first attempt at unit testing. Are my methodologies wrong? Is ExpectedException working as intended and am I abusing it or is there another tool I should use? The only way I can come up with my test is to put the catch try block in the helper and state true when I catch my DataAccessException.

    [TestMethod]
    [ExpectedException(typeof(DataAccessException))]
    public void NHibernateRepositoryBaseDelete()
    {
        NHibernateRepositoryBaseDeleteHelper<Order, int>(myOrder, myOrder.OrderId);
        NHibernateRepositoryBaseDeleteHelper<OrderDetail, int>(myOrderDetail, myOrderDetail.OrderDetailId);
        NHibernateRepositoryBaseDeleteHelper<Schedule, int>(mySchedule, mySchedule.ScheduleId);
    }

    private static void NHibernateRepositoryBaseDeleteHelper<T, TKey>(T myItem, TKey myItemId)
    {
        MyTestRepository<T, TKey> myRepository = new MyTestRepository<T, TKey>();
        myRepository.Delete(myItem);
        myRepository.CommitChanges();

        myRepository.GetById(myItemId, false);
    }
+3
source share
2 answers

I usually don’t use ExpectedExceptionit unless I can force the exception to be thrown into one statement, or if other tests guarantee that the previous statements do not throw an exception.

: , . , ExpectedException , , , , - , , , .

, ( ), :

try
{
    OperationThatShouldFail();
    Assert.Fail("Expected exception");
}
catch (DataAccessException)
{
    // Expected (no need for an assertion though)
}

( ExpectedException - , .)

. (, , ) , ExpectedException, . try/catch .

, , .

EDIT: , , , , , . teardown, ( ).

EDIT: ExpectedException (, , ) , , :

static void ExpectException<T>(Action action) 
    where T : Exception
{
    try
    {
        action();
        Assert.Fail("Expected exception " + typeof(T));
    }
    catch (T)
    {
        // Expected
    }
}

( ) , , , # 3. :

// Method name shortened for simplicity, and I'm assuming that type inference
// will work too.
public void NHibernateRepositoryBaseDelete()
{
    ExpectException<DataAccessException>(() => 
        DeleteHelper(myOrder, myOrder.OrderId));
    ExpectException<DataAccessException>(() => 
       DeleteHelper(myOrderDetail, myOrderDetail.OrderDetailId));
    ExpectException<DataAccessException>(() => 
       DeleteHelper(mySchedule, mySchedule.ScheduleId));
}
+10

unit test try/finally .

[TestMethod]    
[ExpectedException(typeof(DataAccessException))]    
public void NHibernateRepositoryBaseDelete()    
{
    try
    {
        NHibernateRepositoryBaseDeleteHelper<Order, int>(myOrder, myOrder.OrderId);
        NHibernateRepositoryBaseDeleteHelper<OrderDetail, int>(myOrderDetail, myOrderDetail.OrderDetailId);
        NHibernateRepositoryBaseDeleteHelper<Schedule, int>(mySchedule, mySchedule.ScheduleId);
    }
    finally
    {
        // clean up database here
    }   
}
+4

Source: https://habr.com/ru/post/1704684/


All Articles