c# - Unit Testing - How do I test this class -


i have class cache (wraps system.runtime.caching.memorycache) have created has following public methods:

bool add(string key, object value) bool contains(string key) 

how test contains() method without using add() method in first place add item cache??? surely if use add() method within unit test contains() method wrong?

i think understand dilemma. asking if use new method test new method. sort of testing unknown unknown.

to me @ test isolation case of assumption. let me explain...

the following applies if both methods there , responsible testing them.

in first instance, want test add(,) method. in case, 'assume' other methods work correctly. in case, run add(key,value) , check asserts true, , verify call contains(key) , ensure asserts true. though contains() untested.

then in second instance, assume add(,) working , test contains() method. test based on assumption i've got blinkers on , testing specific, specific method, specific class , run on assumption supporting required run test works.

in reality, not case , of supporting methods don't work expected. but, test let know. , that's it's there for. reveal issues.

you have test in mind but, here's quick example can combine testing of these 2 methods single test. of course, can split 2 tests (one thats add() centric , 1 that's contains() centric) similar. hope 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 pure tdd approach, this:

create add(,) method write following test:

   [test]             public void testadd()     {         var instance = new someclass();          const string key = "somekey";         assert.istrue(instance.add(key, new object()));     } 

then create contains() method , write following test:

    [test]     public void testcontains()     {         var instance = new someclass();          const string key = "somekey";         assert.isfalse(instance.contains(key));          // purest don't need following assert         // add not testing.          // don't see harm         assert.istrue(instance.add(key, new object()));         assert.istrue(instance.contains(key));          instance.remove(key);         assert.isfalse(instance.contains(key));     } 

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -