Skip to content

Instantly share code, notes, and snippets.

@huseyint
Created July 26, 2013 09:33
Show Gist options
  • Save huseyint/6087575 to your computer and use it in GitHub Desktop.
Save huseyint/6087575 to your computer and use it in GitHub Desktop.
Dictionary with single item assertion
// headers is a Dictionary<string, string>
Assert.AreEqual(1, headers.Count);
Assert.AreEqual("bar", headers["foo"]);
// Do I need both of these? Only first? Only second?
// Only 1st: What if the item is different than what I expected?
// Only 2nd: If dictionary is empty, this line will throw an exception before assertion is evaluated, is this OK?
// Both: Isn't this against "one assert per unit test" rule?
@huseyint
Copy link
Author

Another option is to use CollectionAssert.AreEquivalent

@emre
Copy link

emre commented Jul 26, 2013

one test per concept is okay but one assert per test is most likely overkill.

@ploeh
Copy link

ploeh commented Jul 26, 2013

I'd do some variation of:

var expected = new Dictionary<string, string> { { "bar", "foo" } };
Assert.True(expected.SequenceEquals(headers));

That states what you expect, and what you verify, in a single assertion, so it addresses all your questions in one go.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment