Adrien Rose Adrien Rose - 11 days ago 7
C# Question

Does NUnit support Nullables?

I am testing a method which maps values from an object to another.
However when it come to nullables, more precisely

bool?
each time the function returns the boolean become null.

Example:

i want to test this method:

public Family CreateFamily(Family a, Father b)
{
// some property mapping here
IsEnable = true; // where IsEnable is a nullable boolean
}


Now this method is used like that:

family x = CreateFamily(para1,para2);


//the unit test is:
x.IsEnable.Should.BeTrue(); // but null was returned


but when i dont used the NUNIT TestRunner the condition is met

Answer

I believe what you should be asking is if the Shouldly assertion framework handles Nullables since that looks like what you are using for assertions. My guess is that it does not.

You asked about NUnit though. The latest version of NUnit does support some nullables. For example, all of the following tests pass in NUnit 3.5,

[Test]
public void TestNullableBooleanTrue()
{
    bool? b = true;
    Assert.That(b, Is.True);
}

[Test]
public void TestNullableBooleanFalse()
{
    bool? b = false;
    Assert.That(b, Is.False);
}

[Test]
public void TestNullableBooleanNull()
{
    bool? b = null;
    Assert.That(b, Is.Null);
}
Comments