The goal of my UnitTest is to validate if the ValidateEquality
will return false for two unequal instances of a FooClass
.
I'm asking only about the arrange part of the unit test while the act and assert are not important.
I have a builder facade that is creating a fake instances of the desired FooClass
(BuilderFacade.BuildFooClass()
) and real service api that creating a real instances of it (GetFooClassFromSomewhere
).
The problem is that the BuilderFacade
creates a random E_BooEnum
value, and in order for this unit test will pass every time I need to instantiate the foolClass2
with a BooEnum
value that is different from the fooClass1
's BooEnum
random value.
How to initiate the BooEnum
with a value except of the fake instance value?
[TestMethod]
public void AreEqual_DifferentBooEnum_ShouldReturnFalse()
{
//Arrange:
FooClass fooClass1 = BuilderFacade.BuildFooClass();
FooClass fooClass2 = GetFooClassFromSomewhere();
fooClass2.BooEnum = ! fooClass1.BooEnum // <--------Every possible value is OK besides the `fooClass1.BooEnum` value.
...
//Act
var result = service.ValidateEquality(fooClass1, fooClass2);
//Assert
Assert.IsFalse(result);
}
public class BuilderFacade
{
public static FooClass BuildFooClass()
{
Random rand = new Random();
E_BooEnum booEnum = (E_BooEnum)rand.Next(System.Enum.GetNames(typeof(E_BooEnum)).Length);
return new FooClass()
{
BooEnum = booEnum
};
}
}
The model:
public class FooClass
{
public E_BooEnum BooEnum { get; set; }
public override in GetHashCode()
{
unchecked
{
int hash = 17;
hash = hash * 31 + this.BooEnum.GetHashCode();
}
}
}