Adrien Rose Adrien Rose - 1 year ago 89
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

each time the function returns the boolean become null.


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 Source

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,

public void TestNullableBooleanTrue()
    bool? b = true;
    Assert.That(b, Is.True);

public void TestNullableBooleanFalse()
    bool? b = false;
    Assert.That(b, Is.False);

public void TestNullableBooleanNull()
    bool? b = null;
    Assert.That(b, Is.Null);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download