Verifying assumptions with tests

Here are a few interesting tests that I just wrote:

[TestFixture]
public class SystemWebCacheTests : CacheMixin
{
	[SetUp]
	public void Setup()
	{
		ClearCache();
	}

	[Test]
	public void IterationWithModifications()
	{
		Cache["foo"] = new object();
		Cache["bar"] = new object();

		foreach (DictionaryEntry de in Cache)
		{
			Cache.Remove(de.Key.ToString());
		}
		Assert.AreEqual(0, Cache.Count);
	}

	[Test]
	public void IterationWithAdditions_ShouldGet_ConsistentSnapshot()
	{
		Cache["foo"] = new object();
		Cache["bar"] = new object();
		bool first = true;
		int count = 0;
		foreach (DictionaryEntry de in Cache)
		{
			count += 1;
			if(first==false)
				Cache["baz"] = new object();
			first = false;
		}
		Assert.AreEqual(2, count);
		Assert.AreEqual(3, Cache.Count);
	}

	[Test]
	public void IterationWithRemoval_ShouldGet_ConsistentSnapshot()
	{
		Cache["foo"] = new object();
		Cache["bar"] = new object();
		bool first = true;
		int count = 0;
		foreach (DictionaryEntry de in Cache)
		{
			count += 1;
			if (first)
				Cache.Remove("bar");
			Assert.IsNotNull(de.Value);
			Assert.IsNotNull(de.Key);
			first = false;
		}
		Assert.AreEqual(2, count);
		Assert.AreEqual(1, Cache.Count);
	}
}

I have no idea if this is just a coincidental evidence, but I like the consistency.

Print | posted on Friday, June 06, 2008 12:11 PM

Feedback


Gravatar

# re: Verifying assumptions with tests 6/6/2008 12:49 PM Glyn Darkin

Can you expand abit on how you implement your "Mixin" in C#


Gravatar

# re: Verifying assumptions with tests 6/7/2008 7:22 AM Ayende Rahien

Mixin in a convention of mine to express that I am using a base class as a way to inject particular functionality, not as an AS A relationship

Comments have been closed on this topic.