Last active
October 30, 2015 01:28
-
-
Save DaveGreasley/f0a25a5a3ad849cceea1 to your computer and use it in GitHub Desktop.
An example of why not to use class level variables in unit tests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Cache | |
{ | |
private Dictionary<string, object> cache; | |
public Cache() | |
{ | |
this.cache = new Dictionary<string, object>(); | |
} | |
public int Count { get { return cache.Count; } } | |
public void AddObject(string key, object data) | |
{ | |
this.cache.Add(key, data); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[TestClass] | |
public class CacheTests | |
{ | |
private Cache testCache; | |
public CacheTests() | |
{ | |
this.testCache = new Cache(); | |
} | |
[TestMethod] | |
public void TestCountReturnsTwoWhenTwoOjectsAdded() | |
{ | |
var testData1 = new object(); | |
var testData2 = new object(); | |
this.testCache.AddObject("testData1", testData1); | |
this.testCache.AddObject("testData2", testData2); | |
Assert.AreEqual(2, this.testCache.Count); | |
} | |
[TestMethod] | |
public void TestCountReturnsThreeWhenThreeOjectsAdded() | |
{ | |
var testData1 = new object(); | |
var testData2 = new object(); | |
var testData3 = new object(); | |
this.testCache.AddObject("testData1", testData1); | |
this.testCache.AddObject("testData2", testData2); | |
this.testCache.AddObject("testData3", testData3); | |
Assert.AreEqual(3, this.testCache.Count); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment