2011-05-20 92 views
0

嗨我有一个简单的方法,其中它需要一个字符串作为参数(域名// firstname.lastname)并取出(domainname //)并返回字符串作为firstname.lastname。单元测试方法ASP.NET帮助?

我需要为以下情况进行单元测试。

  1. 检查单斜杠(域名/ firstname.lastname)
  2. 检查空字符串
  3. 检查NULL值
  4. 检查wheather字符串包含//或不

我写了以下内容,但对我来说没有任何意义,因为这是完全错误的。

#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"; 
     string 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 

我知道我完全错了,但有些可以帮助我理解,怎么写,有什么错误。感谢您的耐心

非常感谢 哈日

+0

什么是你Function.RemoveSlash(源)的功能是什么样子? – gbs 2011-05-20 18:48:35

+0

它需要domainname \\ hari.gillalla并给出hari.gillala – 2011-05-20 19:25:49

+0

但是如果它为null/string.Empty会返回什么? – gbs 2011-05-20 19:31:12

回答

0

你的来源应该是你正在执行测试的值:

例如检查你的源字符串应该包含的双斜杠//。

下面是一个示例测试(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); 
} 

,该测试使用类:

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; 
     } 
    }