简体   繁体   中英

Unit Testing - How do I test this class

I have class Cache (wraps System.Runtime.Caching.MemoryCache ) that I have created which has the following public methods:

bool Add(string key, object value)
bool Contains(string key)

How do I test the Contains() method without using the Add() method in the first place to add an item to the Cache??? Surely if I use the Add() method within the unit test for the Contains() method this would be wrong?

How do I test the Contains() method without using the Add() method in the first place to add an item to the Cache?

Assert that it returns false.

Surely if I use the Add() method within the unit test for the Contains() method this would be wrong?

Of course not. You're testing a unit which has a Contains() that differs in behaviour after you've called Add() . Not having tests for Contains() called after Add() would be wrong.

I think I understand your dilemma. You are asking if you use a new method to test another new method. Sort of testing an unknown with an unknown.

To me I would look at it as test isolation case of assumption. Let me explain...

The following applies if both methods are there and you are responsible for testing them.

In the first instance, you want to test the Add(,) method. In that case, I would 'assume' that all other methods work correctly. So in that case, I would run an Add(key,value) and check that it Asserts true, and then verify with a call to Contains(key) and ensure that it Asserts true. Even though Contains() is untested.

Then in the second instance, you assume that Add(,) is working properly and test the Contains() method. I always test based on the assumption that I've got blinkers on and testing something specific, a specific method, a specific class and run on the assumption that everything supporting required to run the test actually works.

In reality, this is not always the case and some of the supporting methods don't work as expected. But, the test will let you know. And that's what it's there for. To help reveal those issues.

You probably already have this test in mind but, here's a quick example where you can combine the testing of these two methods into a single test. Of course, you can split it into two tests (one thats Add() centric and one that's Contains() centric) but they will look fairly similar. I hope it helps.

public class SomeClass : MemoryCache
{
    public SomeClass() : base("Test")
    {
    }

    public bool Add(string key, object value)
    {
        return Add(key, value, DateTime.Now.AddDays(1));
    }
}

[TestFixture]
public class TestSomeClass
{
    [Test]
    public void TestSomeClassMethod1()
    {
        var instance = new SomeClass();

        const string key = "someKey";
        Assert.IsFalse(instance.Contains(key));

        Assert.IsTrue(instance.Add(key, new object()));
        Assert.IsTrue(instance.Contains(key));

        instance.Remove(key);
        Assert.IsFalse(instance.Contains(key));
    }
}

But in a pure TDD approach, you would do this:

Create the Add(,) method then write the following test:

   [Test]        
    public void TestAdd()
    {
        var instance = new SomeClass();

        const string key = "someKey";
        Assert.IsTrue(instance.Add(key, new object()));
    }

Then you would create the Contains() method and then write the following test:

    [Test]
    public void TestContains()
    {
        var instance = new SomeClass();

        const string key = "someKey";
        Assert.IsFalse(instance.Contains(key));

        // A purest will say that you don't need the following Assert
        // for the Add as it is not what you are testing. 
        // But I don't see any harm
        Assert.IsTrue(instance.Add(key, new object()));
        Assert.IsTrue(instance.Contains(key));

        instance.Remove(key);
        Assert.IsFalse(instance.Contains(key));
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM