views:

77

answers:

3

Suppose I need to add unit tests for the following class from legacy code (that has no unit test for now). It is just a simple map or dictionary.

function Map(...) { ... }
Map.prototype.put = function (key, value) {
    // associate the value with the key in this map
}
Map.prototype.get = function (key) {
    // return the value to which the specified key is mapped, or undefined 
    // if this map contains no mapping for the key
}
Map.prototype.equals = function (obj) { ... }
// ... and more bound functions

It seems there is no way to test only one function at a time. You cannot test get() without calling put(), for example. How do I unit test this?

A: 

if you are working with data base, for the "get" method you can create DbScripts with inserts in your data base and then get those inserted items. then you have to create DbScripts for deleting those added items. for the "put" test you'll have to call the get method to check if it was inserted.

you just have to configure this in your test base class.

[TestFixtureSetUp]
        public virtual void InitializeTestData()
        {
            TestConfiguration.ExecuteSqlFilesInFolder(this.DefaultScriptDirectory + "\\SetUp");
            if (!string.IsNullOrEmpty(this.TestFixtureSpecificScriptDirectory))
            {
                TestConfiguration.ExecuteSqlFilesInFolder(this.TestFixtureSpecificScriptDirectory + "\\SetUp");
            }
        }

[TestFixtureTearDown]
        public virtual void FinalizeTestData()
        {
            if (!string.IsNullOrEmpty(this.TestFixtureSpecificScriptDirectory))
            {
                TestConfiguration.ExecuteSqlFilesInFolder(this.TestFixtureSpecificScriptDirectory + "\\TearDown");
            }

            TestConfiguration.ExecuteSqlFilesInFolder(this.DefaultScriptDirectory + "\\TearDown");
        }
Nicole
A: 

Each method has a contract, explicit or implicit. Map.put() takes some kind of input and mutates something internal or external to Map. In order to test that function, your test needs access to what is mutated. If it is internal and not exposed externally, your test must either exist inside the Map class, the state must be exposed, or the mutateable state structure must be injected into the class in a way that external access remains possible: ie:

/*Definition*/

function MockRepository() { /*implementation of the repository*/ }
function Map(repository) { /* store the repository */ }
Map.prototype.put = function() { /* mutate stuff in the repository */ }

/*Instantiation/Test*/
var mockRepository = new MockRepository(); /*mock repository has public methods to check state*/
var myMap = new Map(mockRepository);
myMap.put(/*whatever test input*/);
/* here use the mock repository to check that mutation of state occurred as expected based on ititial state of respository and input */
David
+1  A: 

If there is a heavy dependancy between the methods you could stub or mock out all the other methods.. Have a look at jsMock for this.

Mongus Pong
I said 'could' above... I meant 'should'!
Mongus Pong