Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save DaveGreasley/f0a25a5a3ad849cceea1 to your computer and use it in GitHub Desktop.
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
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);
}
}
[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