Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
单元测试-Method-ASP.NET-Help?_Asp.net - Fatal编程技术网

单元测试-Method-ASP.NET-Help?

单元测试-Method-ASP.NET-Help?,asp.net,Asp.net,您好,我有一个简单的方法,它将字符串作为参数(domainname//firstname.lastname),取出(domainname//),并返回字符串作为firstname.lastname 我需要对以下情况进行单元测试 检查单斜杠(域名/firstname.lastname) 检查是否有空字符串 检查空值 检查字符串是否包含//或不包含 我写了以下内容,但对我来说没有任何意义,因为这是完全错误的 #region Checking for null value [TestMetho

您好,我有一个简单的方法,它将字符串作为参数(domainname//firstname.lastname),取出(domainname//),并返回字符串作为firstname.lastname

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

  • 检查单斜杠(域名/firstname.lastname)
  • 检查是否有空字符串
  • 检查空值
  • 检查字符串是否包含//或不包含
  • 我写了以下内容,但对我来说没有任何意义,因为这是完全错误的

    #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
    
    我知道我完全错了,但有些可以帮助我理解,如何写,有哪些错误。谢谢你的耐心

    非常感谢
    Hari

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

    例如,检查源字符串是否包含双斜杠/

    这是一个样本测试(NUnit)

    测试使用的类:

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

    您的Function.RemoveSlash(source)函数是什么样子的?它接受domainname\\hari.gillala并给出hari.gillala,但如果为null/string.Empty,它将返回什么?
    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;
            }
        }