Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 145769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T08:28:57+00:00 2026-05-11T08:28:57+00:00

I’m testing the delete method of an abstract base repository class which is inherited

  • 0

I’m testing the delete method of an abstract base repository class which is inherited by several other repository classes. MyTestRepository inherits the base so I can run tests against the base’s methods without restricting my test to using a concrete class. When I run my unit test it passes, but I noticed afterwards I have several OrderDetail and Schedule objects in the test DB that were generated by the test (objects get created during test initialization) and not deleted, while the Order object is deleted. I added some breakpoints and noticed that as soon as the helper method ends and the expected exception has been thrown the test ends and the 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 I’m misusing it or is there another tool I should be using? The only way I can think of to get my test is to put a try catch block in the helper and assert 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);     } 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T08:28:58+00:00Added an answer on May 11, 2026 at 8:28 am

    I don’t generally use ExpectedException unless I can cause the exception to be thrown in a single statement – or if other tests ensure that the earlier statements don’t throw the exception.

    Here, you’ve basically got three tests – you’re testing that each of those delete calls will throw an exception. All that ExpectedException does is run the method and check that it threw the exception you asked it to – it doesn’t try to continue from where the exception was thrown, expecting it to be thrown again.

    If you want to check that an exception is thrown by a specific piece of code (rather than just the whole method) use:

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

    (And don’t have ExpectedException any more – you no longer expect the test method to throw.)

    You’d have one of these blocks for each of the three checks. Alternatively (and probably better) just have three tests, each of which uses ExpectedException but is only one line long. As another alternative, you could put the try/catch into your helper method.

    You might also want to have an assertion at the end of the test that the relevant tables are empty – but that depends on your situation.

    EDIT: As for when you clean up the database – I generally like to clean it at the start of each test so that if I run just a single failing test I can see the state of the database afterwards. If I were to clean it up in the teardown method, I’d have lost valuable information (or be forced to stay in the debugger).

    EDIT: Another alternative to ExpectedException (which I suspect is in many test frameworks now) is to have a generic method like this:

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

    You can then call it easily (multiple times) from within the method using a lambda expression for the action, assuming you’re using C# 3. For example:

    // 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)); } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 69k
  • Answers 69k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Going to answer, because I hate seeing that red block… May 11, 2026 at 12:29 pm
  • added an answer If you have a member initialized like that, it will… May 11, 2026 at 12:29 pm
  • added an answer Trust me: you really don't want to maintain your bugs,… May 11, 2026 at 12:29 pm

Related Questions

I keep getting tasks that are above my skill level. How can I address this without coming accross as grossly incompetent?
I have a web-service that I will be deploying to dev, staging and production.
I'm thinking of starting a wiki, probably on a low cost LAMP hosting account.
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.