代码之家  ›  专栏  ›  技术社区  ›  John MacIntyre

如何忽略Moq VerifySet()表达式中的空白?

  •  2
  • John MacIntyre  · 技术社区  · 14 年前

    我想验证字符串是否被设置为Moq对象中的特定值。

    我创建了一个小控制台应用程序来模拟我想要做的事情。

    using System;
    using Moq;
    
    namespace MoqVerifySet
    {
        public interface MyInterface
        {
            string MyValue { get; set; }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                Mock<MyInterface> mockMyInterface = new Mock<MyInterface>();
                var myI = mockMyInterface.Object;
                myI.MyValue = @"hello 
                                world.
                                Please ignore
                                the whitespace";
    
                try
                {
                    mockMyInterface.VerifySet(i => i.MyValue = "hello world. Please ignore the whitespace");
                    Console.WriteLine("Success");
                }
                catch(Exception ex)
                {
                    Console.WriteLine("Error : {0}", ex.Message);
                }
    
                Console.WriteLine("\n\nPress any key to exit...");
                Console.ReadKey();
            }
        }
    }
    

    public static string PrepSqlForComparison(string sql)
    {
        Regex re = new Regex(@"\s+");
        return re.Replace(sql, " ").Trim().ToLower();
    }
    

    和改变

    mockMyInterface.VerifySet(i => i.MyValue = "hello world. Please ignore the whitespace");
    

    mockMyInterface.VerifySet(i => PrepSqlForComparison(i.MyValue) = "hello world. Please ignore the whitespace");
    

    但这不会编译,因为表达式中的运算符是赋值,而不是等于。

    所以,如果我不能这样做,如何在忽略大小写、空白和其他格式的情况下进行验证?

    2 回复  |  直到 14 年前
        1
  •  4
  •   C8H10N4O2    14 年前

    如果不需要VerifySet的功能(如果只关心最后一个设置值),可以在模拟对象中设置MyValue属性,通过调用:

    mockMyInterface.SetupProperty(f => f.MyValue);
    

    然后可以执行比较/相等检查,忽略空白和换行符。我创建了一个自定义的StringComparer来在测试中封装此逻辑:

    using System;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using Moq;
    
    namespace MoqVerifySet
    {
    public interface MyInterface
    {
        string MyValue { get; set; }
    }
    
    internal class Program
    {
        private static void Main(string[] args)
        {
            var mockMyInterface = new Mock<MyInterface>();
            mockMyInterface.SetupProperty(f => f.MyValue);
    
            MyInterface myI = mockMyInterface.Object;
            myI.MyValue = @"hello 
                            world.
                            Please ignore
                            the whitespace";
            try
            {
                var comparer = new CompareWithoutWhitespace(StringComparison.CurrentCultureIgnoreCase);
                Assert.IsTrue(comparer.Equals(myI.MyValue, "hello world. Please ignore the whitespace"));
                Console.WriteLine("Success");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error : {0}", ex.Message);
            }
    
            Console.WriteLine("\n\nPress any key to exit...");
            Console.ReadKey();
        }
    }
    
    internal class CompareWithoutWhitespace : StringComparer
    {
        private readonly StringComparison _stringComparison;
    
        public CompareWithoutWhitespace(StringComparison stringComparison)
        {
            _stringComparison = stringComparison;
        }
    
        public override int Compare(string x, string y)
        {
            return String.Compare(RemoveWhitespace(x), RemoveWhitespace(y), _stringComparison);
        }
    
        public override bool Equals(string x, string y)
        {
            return String.Equals(RemoveWhitespace(x), RemoveWhitespace(y), _stringComparison);
        }
    
        public override int GetHashCode(string obj)
        {
            return RemoveWhitespace(obj).GetHashCode();
        }
    
        private static string RemoveWhitespace(string input)
        {
            if (string.IsNullOrEmpty(input)) return input;
            input = input.Replace(Environment.NewLine, "");
            return input.Replace(" ", "");
        }
    }
    }
    
        2
  •  1
  •   IanNorton    13 年前

    您的空白问题与Mock无关,myI.MyValue正在使用 verbatim string literal .

    台词:

    myI.MyValue = @"hello world.
                    Please ignore
                    the whitespace";
    

    注意最后两行第一个字母左边的所有空格。相当于书写:

    myI.MyValue = @"hello world.                            Please ignore                            the whitespace";
    
    推荐文章