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 6472609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:21:58+00:00 2026-05-25T06:21:58+00:00

When applying TDD, do you create tests that verify expected exceptions for arguments (ArgumentException,

  • 0

When applying TDD, do you create tests that verify expected exceptions for arguments (ArgumentException, ArgumentNullException, InvalidOperation, etc.) or just ones that are “known”, for example, CustomerDelinquentException ?

What about equals, gethashcode overrides? how would i test gethashcode?

Thanks

  • 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. Editorial Team
    Editorial Team
    2026-05-25T06:21:59+00:00Added an answer on May 25, 2026 at 6:21 am

    I always test for any exceptions I throw in my method, including ArgumentNullException, ArgumentException, etc. I just find it’s best to test these and they’re easy to write. That way if someone ever removes those guards, the tests would break and you’d know.

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void ToSetOnNullThrows()
        {
            List<string> list = null;
            var target = list.ToHashSet();
        }
    

    As far as GetHashCode() and Equals() i test these as well if you override them. For GetHashCode() an easy test is to create two equivalent objects (same values used in the hash code) and prove that the hash codes both objects generate are the same.

        [TestMethod]
        public void GetHashCodeSameKeysAreSameTest()
        {
            var key = new CompositeKey<string, int>("A", 13);
            var otherKey = new CompositeKey<string, int>("A", 13);
    
            Assert.AreEqual(key.GetHashCode(), otherKey.GetHashCode());
        }
    

    You can try to test that two non-equivalent objects return different hash codes, but you’d have to make sure the values you use aren’t simply a collision. This largely depends on the algorithm you code in GetHashCode().

        [TestMethod]
        public void GetHashCodeDifferentKeysAreMostLikelyDifferentTest()
        {
            var key = new CompositeKey<string, int>("A", 13);
            var otherKey = new CompositeKey<string, int>("A", 14);
    
            Assert.AreNotEqual(key.GetHashCode(), otherKey.GetHashCode());
        }
    

    For Equals() test that two equivalent objects with the same fields return true on Equals() and false on two non-equivalent objects.

        [TestMethod]
        public void EqualsTest()
        {
            var key = new CompositeKey<string, int>("A", 13);
            var otherKey = new CompositeKey<string, int>("A", 13);
    
            Assert.IsTrue(key.Equals(otherKey));
        }
    
        [TestMethod]
        public void NotEqualsTest()
        {
            var key = new CompositeKey<string, int>("A", 13);
            var otherKey = new CompositeKey<string, int>("A", 15);
    
            Assert.IsFalse(key.Equals(otherKey));
        }
    

    For even more fun, I like to unit tests DateTime dependent stuff too. This is somewhat harder, but if a method’s behavior depends on DateTime i still want to unit test them. So you can create a DateTime generator delegate that defaults to returning DateTime.Now but have it so that you can set the generator to a specific DateTime. Helps a lot with coverage in my line of work since I’m in the financial industry and a lot of logic depends on pre-market, post-market hours, etc…

    public class SomeClassThatDependsOnCurrentTime
    {
        internal Func<DateTime> NowGenerator { get; set; }
    
        public SomeClassThatDependsOnCurrentTime()
        {
            // default in constructor to return DateTime.Now
            NowGenerator = () => DateTime.Now;
        }
    
        public bool IsAfterMarketClose()
        {
            // call the generator instead of DateTime.Now directly...
            return NowGenerator().TimeOfDay > new TimeSpan(16, 0, 0);
        }
    }
    

    Then you just set up a unit test to inject a specific date time.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to learn TDD by applying it to a simple project of mine.
As applying unit-test to some C code, we run into a problem that some
I'm struggling with applying the visitor pattern on some objects that have scalar members
I have a JInternalFrame that I am applying a custom UI to. The UI
Hi im applying custom images to button on different states, for which i create
I'm currently applying anonymous functions to a hover event, that call global functions inside:
How to approach applying for a job at a company owned by a friend?
I have a problem applying the DebuggerDisplay attribute on a generic class: [DebuggerDisplay(--foo--)] class
The backstory: I am applying a filter to a TableView. It's a simple 'find
Multisampling is a way of applying full screen anti-aliasing (FSAA) in 3D applications. I

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.