开发者

Unit Testing-Method-ASP.NET-Help?

开发者 https://www.devze.com 2023-03-07 03:23 出处:网络
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.

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.

  1. check for single slash (domainname/firstname.lastname)
  2. Check for empty string
  3. Check for null value
  4. 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;
        }
    }
0

精彩评论

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