Hi I have a simple method, where It takes a string as parameter (domainname//firstname.lastname) and takes out (domainname//) and returns string as just firstname.lastname.
I need to unit test for the following case.
- check for single slash (domainname/firstname.lastname)
- Check for empty string
- Check for null value
- Check wheather the string contains // or not
I wrote the following but it does not make any sense to me, coz this is completely wrong.
#region Checking for null value
[TestMethod]
public void CheckForNullValueTest()
{
string expected = "";
string source = @"domainname//hari.gillala";
string actual = Function.RemoveSlash(source);
assert.Equal(expected, actual);
}
#endregion
#region Checking for empty
[TestMethod]
public void CheckForEmptyValueTest()
{
string expected = string.empty;
string source = @"domainname//hari.gillala";
string actual = Function.RemoveSlash(source);
assert.Equal(expected, actual);
}
#endregion
#region Checking for singleslash
[TestMethod]
public void CheckForEmptyValueTest()
{
string expected = @"domainname/hari.gillala";
string source = "domainname//hari.gillala";
strin开发者_JAVA技巧g actual = Function.RemoveSlash(source);
assert.Equal(expected, actual);
}
#endregion
#region Checking for Doubleslash
[TestMethod]
public void CheckForEmptyValueTest()
{
string expected = @"domainname//hari.gillala";
string source = "domainname//hari.gillala";
string actual = Function.RemoveSlash(source);
assert.Equal(expected, actual);
}
#endregion
I know I am completely wrong, but some can help me to understand, how to write, what are the mistakes. thanks for the patience
Many Thanks Hari
Your source should be a value you are performing test for:
E.g. Checking for double slash your source string should contain //.
Here's a Sample test (NUnit).
[Test]
public void CheckForDoubleSlashValueTest()
{
string expected = "hari.gillala";
string source = @"domainname//hari.gillala";
string result = MyClass.GetString(source);
Assert.AreEqual(expected, result);
}
[Test]
public void CheckForSingleSlashValueTest()
{
string expected = "hari.gillala";
string source = @"domainname/hari.gillala";
string result = MyClass.GetString(source);
Assert.AreEqual(expected, result);
}
[Test]
public void CheckForEmptyValueTest()
{
string expected = String.Empty;
string source = String.Empty;
string result = MyClass.GetString(source);
Assert.AreEqual(expected, result);
}
[Test]
public void CheckForNullValueTest()
{
string expected = String.Empty;
string source = null;
string result = MyClass.GetString(source);
Assert.AreEqual(expected, result);
}
The Class that the Test uses:
public static class MyClass
{
//Returns empty string if source you pass is null or String.Empty
public static string GetString(string s)
{
string name = String.Empty;
if(!String.IsNullOrEmpty(s)){
string[] names = s.Split('/');
name = names[names.Length - 1];
}
return name;
}
}
精彩评论