【问题标题】:How can I ignore white space in a Moq VerifySet() expression?如何忽略 Moq VerifySet() 表达式中的空格?
【发布时间】:2010-11-06 04:24:13
【问题描述】:

我想验证一个字符串是否被设置为 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");

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

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

【问题讨论】:

    标签: c# unit-testing mocking moq


    【解决方案1】:

    如果您不需要 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(" ", "");
        }
    }
    }
    

    【讨论】:

    • 谢谢。这正是我一直在寻找的。 +1
    【解决方案2】:

    您的空格问题与 Mock 无关,您的 myI.MyValue 使用的是verbatim string literal

    行:

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

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

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

    【讨论】:

      猜你喜欢
      • 2021-09-13
      • 2020-08-06
      • 2011-08-12
      • 2021-05-31
      • 1970-01-01
      • 2018-11-23
      • 2011-06-03
      相关资源
      最近更新 更多