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

  • Home
  • SEARCH
  • 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 303595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:12:10+00:00 2026-05-12T07:12:10+00:00

Consider the following class: public class MyIntSet { private List<int> _list = new List<int>();

  • 0

Consider the following class:

public class MyIntSet
{
    private List<int> _list = new List<int>();

    public void Add(int num)
    {
        if (!_list.Contains(num))
            _list.Add(num);
    }

    public bool Contains(int num)
    {
        return _list.Contains(num);
    }
}

Following the “only test one thing” principle, suppose I want to test the “Add” function.
Consider the following possibility for such a test:

[TestClass]
public class MyIntSetTests
{
    [TestMethod]
    public void Add_AddOneNumber_SetContainsAddedNumber()
    {
        MyIntSet set = new MyIntSet();
        int num = 0;

        set.Add(num);
        Assert.IsTrue(set.Contains(num));
    }
}

My problem with this solution is that it actually tests 2 methods: Add() and Contains().
Theoretically, there could be a bug in both, that only manifests in scenarios where they are not called one after the other. Of course, Contains() now servers as a thin wrapper for List’s Contains() which shouldn’t be tested in itself, but what if it changes to something more complex in the future? Perhaps a simple “thin wrap” method should always be kept for testing purposes ?

An alternative approach might suggest mocking out or exposing (possibly using InternalsVisibleTo or PrivateObject) the private _list member and have the test inspect it directly, but that could potentially create test maintainability problems if someday the internal list is replaced by some other collection (maybe C5).

Is there a better way to do this?
Are any of my arguments against the above implementations flawed?

Thanks in advance,
JC

  • 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-12T07:12:11+00:00Added an answer on May 12, 2026 at 7:12 am

    Your test seems perfectly OK to me. You may have misunderstood a principle of unit testing.

    A single test should (ideally) only test one thing, that is true, but that does not mean that it should test only one method; rather it should only test one behaviour (an invariant, adherence to a certain business rule, etc.) .

    Your test tests the behaviour “if you add to a new set, it is no longer empty”, which is a single behaviour :-).

    To address your other points:

    • Theoretically, there could be a bug in both, that only manifests in scenarios where they are not called one after the other.
      True, but that just means you need more tests :-). For example, add two numbers, then call Contains, or call Contains without Add.

    • An alternative approach might suggest mocking out or exposing (possibly using InternalsVisibleTo) the private _list member and have the test inspect it directly, but that could potentially create test maintainability problems[…]
      Very true, so don’t do this. A unit test should always be against the public interface of the unit under test. That’s why it’s called a unit test, and not a “messing around inside a unit”-test ;-).

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

Sidebar

Related Questions

Consider the following class: public class Event<T> { public delegate void Handler<t>(t msg); private
Consider following class class test { public: test(int x){ cout<< test \n; } };
Consider following example : public class SomeBusinessLayerService : DataService<MyEntityContainer> { [WebInvoke] void DoSomething(string someParam)
Consider the following class public class PlanetKrypton { public static void CallSuperManforHelp(string helpMessage, params
Consider the following class public class Class1 { public int A { get; set;
Consider the following code: public class Vehicle { public void StartEngine() { // Code
Consider the following code : public class RandomClass { private readonly string randomString; public
Consider the following code: public class Bar { Foo foo; void Go() { foo
Consider the following: class A { public: const int c; // must not be
Consider the following code: abstract class SomeClassX<T> { // blah } class SomeClassY: SomeClassX<int>

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.