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

C# 如何忽略Moq VerifySet()表达式中的空白?,c#,unit-testing,mocking,moq,C#,Unit Testing,Mocking,Moq,我想验证一个字符串是否被设置为Moq对象中的特定值 我创建了一个小控制台应用程序来模拟我想要做的事情 using System; using Moq; namespace MoqVerifySet { public interface MyInterface { string MyValue { get; set; } } class Program { static void Main(string[] args)

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

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

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


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

如果您不需要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(" ", "");
    }
}
}
使用系统;
使用Microsoft.VisualStudio.TestTools.UnitTesting;
使用最小起订量;
名称空间MoqVerifySet
{
公共接口MyInterface
{
字符串MyValue{get;set;}
}
内部课程计划
{
私有静态void Main(字符串[]args)
{
var mockMyInterface=new Mock();
mockMyInterface.SetupProperty(f=>f.MyValue);
MyInterface myI=mockMyInterface.Object;
myI.MyValue=@“您好
世界
请忽略
“空白”;
尝试
{
var comparer=不带空格的新比较(StringComparation.CurrentCultureIgnoreCase);
Assert.IsTrue(comparer.Equals(myI.MyValue,“你好,世界,请忽略空白”);
Console.WriteLine(“成功”);
}
捕获(例外情况除外)
{
WriteLine(“错误:{0}”,例如Message);
}
Console.WriteLine(“\n\n按任意键退出…”);
Console.ReadKey();
}
}
内部类CompareWithoutWhitespace:StringComparer
{
私有只读StringComparison\u StringComparison;
不带空格的公共比较(StringComparison StringComparison)
{
_stringComparison=stringComparison;
}
公共覆盖整型比较(字符串x、字符串y)
{
返回String.Compare(RemoveWhitespace(x),RemoveWhitespace(y),\u stringComparison);
}
公共覆盖布尔等于(字符串x、字符串y)
{
返回字符串.Equals(RemoveWhitespace(x)、RemoveWhitespace(y)、_stringComparison);
}
公共覆盖int GetHashCode(字符串obj)
{
返回RemoveWhitespace(obj).GetHashCode();
}
私有静态字符串删除空白(字符串输入)
{
if(string.IsNullOrEmpty(input))返回输入;
input=input.Replace(Environment.NewLine,“”);
返回输入。替换(“,”);
}
}
}

您的空白问题与Mock无关,您的myI.MyValue使用了一个

该行:

myI.MyValue=@“你好,世界”。
请忽略
“空白”;
请注意最后两行的第一个字母左边的所有空格。相当于写作:

myI.MyValue=@“你好,世界,请忽略空白”;

谢谢。这正是我想要的+1.
mockMyInterface.SetupProperty(f => f.MyValue);
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(" ", "");
    }
}
}