开发者

Testing string value of Nothing is equivalent to String.Empty

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 fet开发者_如何学Cch 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?


String.IsNullOrEmpty()


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.


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")
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜