【发布时间】: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