Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assert.AreEqual - Null vs Empty String

Tags:

c#

nunit

Take the following example:

string x = null;
var y = String.Empty;
Assert.AreEqual(x, y);             // test fails!
Assert.AreEqual(x ?? "", y ?? ""); // passes, but ugly!

I understand that null and empty string aren't the same but for my particular application these are basically the same. My question is - are there any shortcuts that will treat null and empty string the same way? Otherwise I am faced with using the null coalescing operator all over (a bit ugly!)

Background

For a background on where this question comes from, I'm writing integration tests for a project. Basically I'm saving values to a database, then loading them again and checking that the two match. Some values, when saved are empty strings, but when loaded via EF they are coming back as null values. This causes a number of my integration test to fail.

EDIT/NOTE - The values I am using for testing are randomized. That means sometimes the value will be empty but other times they will be filled in. That in mind, I would have to use the null coalescent on every single string comparison (hence the question of an abbreviated way of doing this).

like image 357
drew_w Avatar asked Jul 30 '14 15:07

drew_w


People also ask

Is null better than empty string?

So, NULL is better. An empty string is useful when the data comes from multiple resources. NULL is used when some fields are optional, and the data is unknown.

Is empty string and null are same?

An empty string is a string instance of zero length, whereas a null string has no value at all. An empty string is represented as "" . It is a character sequence of zero characters. A null string is represented by null .

Does empty string evaluate to null?

An empty string is a String object with an assigned value, but its length is equal to zero. A null string has no value at all. A blank String contains only whitespaces, are is neither empty nor null , since it does have an assigned value, and isn't of 0 length.

How does assert Areequal work?

Tests whether the specified objects are equal and throws an exception if the two objects are not equal. Different numeric types are treated as unequal even if the logical values are equal. 42L is not equal to 42.


1 Answers

Drew: AlexD was on track, although he did not have it fully ferreted out. Example:

    static void Main(string[] args)
    {
        Console.WriteLine(AreSame("", ""));
        Console.WriteLine(AreSame("", null));
        Console.WriteLine(AreSame(null, ""));
        Console.WriteLine(AreSame(null, null));

        Console.Read();
    }

    private static bool AreSame(string x, string y)
    {
        return (string.IsNullOrEmpty(x) == string.IsNullOrEmpty(y));
    }

In your case, you can use

Assert.AreEqual(string.IsNullOrEmpty(x), string.IsNullOrEmpty(y));

Or

Assert.IsTrue(AreSame(x, y));

Ugly? Sure, but you can at least abstract the ugliness out a bit. Ugly as

Assert.AreEqual(x ?? "", y ?? ""); // passes, but ugly!

??? Not sure. Depends on how you look at it.

like image 55
Gregory A Beamer Avatar answered Oct 03 '22 07:10

Gregory A Beamer