This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

.NET 1.1+

NUnit Identity Assertions

The seventh part of the Automated Unit Testing tutorial continues the examination of the assertion commands provided by the NUnit framework. This article describes the identity assertions, which check the references held by objects.


Assert.AreNotSame provides the reverse functionality of the AreSame method. The expected and actual values are compared to ensure that they do not share a reference. If they are a match, the test fails.

The test below asserts that the resolved object is not the same as another object with the same value but a different reference:

public void ResolvedObjectIsNotObjectOfSameValue()
    object similar = new object();
    Assert.AreNotSame(_registeredObject, similar);


The final identity assert is the Assert.Contains method. This assert can be used with both reference types and value types. It checks if an expected value exists within an array or a collection that implements the IList interface. If the value is in the list the test passes.

In the test below, we assert that the value registered in the SetUp method exists within the dictionary of registered types.

public void RegisteredTypeIsInDictionary()
    Assert.Contains(_registeredObject, IoC.RegisteredTypes.Values);
11 April 2011