tags:

views:

48

answers:

3

I have a nullable database field of type varchar. I am writing a unit test for a service method that retrieves the value of this field. The service method uses a custom DAL that returns a value of String.Empty if the database field is Null (this is desired behaviour).

In my unit test, I am using a Linq To SQL autogenerated class in order to fetch the actual database value and test it against my service method value. The L2S class assigns the string a value of Nothing if the database field is Null. I am using Assert.Equal to compare the two values, however the assertion fails because a string value of Nothing is not equivalent to an empty String. I want this assertion to pass because they both represent a null database value!

For most database types, the L2S class will expose nullable fields as a Nullable (of T) type, so I would normally test equality simply like this (VB code):

Assert.AreEqual(l2sValue.GetValueOrDefault, myValue, "Values not equivalent.")

However nullable varchars are just exposed as Strings so I can't use GetValueOrDefault on them. So my question is, what is the most elegant way to alter my code to recognise that a string value of Nothing and String.Empty are equivalent for the purposes of this test?

+1  A: 
String.IsNullOrEmpty()
David Pfeffer
Yep that was my first thought, but I don't see an elegant way to use this without splitting into two test cases using an If statement? Is there a way to avoid this?
elwy
A: 

Unlike C#, string comparisons in VB.Net treat Nothing as equal to empty string, so just use this

Assert.IsTrue(s1 = s2, "Values are not equivalent")
MarkJ
Downside of this is that the assertion gives me no information about why it failed, unlike Assert.AreEqual.
elwy
In that case, I suggest writing your own routine `AssertStringsAreEquivalent` so that it gives whatever behaviour you wish. I guess one drawback is that the assertions will fire in your new routine not in the routine failing the test. You could try using `DebuggerHiddenAttribute` on the new routine http://msdn.microsoft.com/en-us/library/h5e30exc.aspx
MarkJ
It's odd that `StringAssert` doesn't offer a method to help with this. http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting.stringassert_members(VS.80).aspx
MarkJ
Thanks for the links Mark, I am experimenting with creating my own procedures for asserting that two values are equivalent, using generics with overloads for special cases such as strings and nullable types. Then as you point out, point of failure becomes a potential concern.
elwy
+1  A: 

Why isn't the database set to default to an empty string, not NULL, if there is no value provided? If your default is '', tell the DB—it won't know otherwise. This should save you a lot of trouble in the long run.

Samir Talwar
I haven't got the guts to tell my DB Admin that he needs to add default values to all the thousands of fields in his 5-year-old database :). Also I'm hoping that our old custom DAL will get replaced with a nice L2S or EF solution in the future.
elwy
I was hoping that wasn't the reason, but unfortunately, it makes sense. Code solution it is, then.
Samir Talwar