patterncsharpMinor
Am I missing any unit tests in this base class for testing equality?
Viewed 0 times
thisequalityanyteststestingformissingclassunitbase
Problem
I've created this abstract base class for testing the
The basic idea is that derived class implements methods to get:
the derived class to ensure that this list is sufficient instances for testing
all possible inequalities)
The base class then is responsible for using those instances to test.
Am I missing any unit tests that would be useful for ensuring that the class is correctly using
```
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
///
/// A base class for testing the Equals method of an object.
///
[TestClass]
public abstract class EqualityTestBase
{
[TestMethod]
public void Equals_PassNull_ReturnFalse()
{
// -------- Assemble --------
object target = GetPrimary();
bool expected = false;
// -------- Act --------
bool actual = target.Equals(null);
// -------- Assert --------
Assert.AreEqual(expected, actual);
}
[TestMethod]
public void Equals_PassGenericObject_ReturnFalse()
{
// -------- Assemble --------
object target = GetPrimary();
object genericObject = new object();
bool expected = false;
// -------- Act --------
bool actual = target.Equals(genericObject);
// -------- Assert --------
Assert.AreEqual(expected, actual);
}
[TestMethod]
public void Equals_EquivalentObject_ReturnTrue()
{
// -------- Assemble --------
object target = GetPrimary();
object equivalent = GetPrimary();
bool expected = true;
// -------- Act --------
bool actual = target.Equals(equivalent);
// -------- Assert --------
Assert.AreEqual(expected, actual);
}
[TestMethod]
public void Equals_SameObject_ReturnTrue()
{
// -------- Assemble --------
Equals method.The basic idea is that derived class implements methods to get:
- the primary test instance, and
- a list of unequal instances (it's up to
the derived class to ensure that this list is sufficient instances for testing
all possible inequalities)
The base class then is responsible for using those instances to test.
Am I missing any unit tests that would be useful for ensuring that the class is correctly using
Equals and GetHashCode?```
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
///
/// A base class for testing the Equals method of an object.
///
[TestClass]
public abstract class EqualityTestBase
{
[TestMethod]
public void Equals_PassNull_ReturnFalse()
{
// -------- Assemble --------
object target = GetPrimary();
bool expected = false;
// -------- Act --------
bool actual = target.Equals(null);
// -------- Assert --------
Assert.AreEqual(expected, actual);
}
[TestMethod]
public void Equals_PassGenericObject_ReturnFalse()
{
// -------- Assemble --------
object target = GetPrimary();
object genericObject = new object();
bool expected = false;
// -------- Act --------
bool actual = target.Equals(genericObject);
// -------- Assert --------
Assert.AreEqual(expected, actual);
}
[TestMethod]
public void Equals_EquivalentObject_ReturnTrue()
{
// -------- Assemble --------
object target = GetPrimary();
object equivalent = GetPrimary();
bool expected = true;
// -------- Act --------
bool actual = target.Equals(equivalent);
// -------- Assert --------
Assert.AreEqual(expected, actual);
}
[TestMethod]
public void Equals_SameObject_ReturnTrue()
{
// -------- Assemble --------
Solution
It seems to be complete but keep in mind that equality is reflexive, symmetric, and transitive so, as a formality, I would add tests:
a.Equals(b)has the same value asb.Equals(a)
- if
a.Equals(b)andb.Equals(c)are both true, thena.Equals(c)is also true
Context
StackExchange Code Review Q#39684, answer score: 5
Revisions (0)
No revisions yet.