据了解:
Expression<Func<string, bool>> first = x => x.Length == 4;
Expression<Func<string, bool>> second = x => x.Length == 4;
Console.WriteLine(first.Equals(second)); // Output is "False"
但是,检查每个表达式的字符串确实显示相等:
Expression<Func<string, bool>> first = x => x.Length == 4;
Expression<Func<string, bool>> second = x => x.Length == 4;
Console.WriteLine(first.ToString().Equals(second.ToString())); // Output is "True"
这个想法是不同帖子的高潮......
http://www.codethinked.com/Comparing-Simple-Lambda-Expressions-With-Moq Moq'ing methods where Expression<Func<T, bool>> are passed in as parameters Verify method was called with certain linq expression (moq)
意图:
我正在使用存储库模式编写MVC应用程序,以便
public class MyController : Controller
{
public Repository.IRepository Repository { get; set; }
public MyController()
{
this.Repository = new Repository.CommonRepository();
}
public MyController(Repository.IRepository repository)
{
this.Repository = repository;
}
[HttpPost]
public ActionResult Create(Domain.Common.Object1 o1)
{
if (ModelState.IsValid)
{
// Additional validation
o1.Name = o1.Name.Trim();
if (this.Repository.Any<Domain.Common.Object1>(a => a.Name.ToLower() == plant.Name.ToLower()))
this.ModelState.AddModelError("Name", "Duplicate found.");
}
if (ModelState.IsValid)
{
var entity = this.Repository.Add(o1);
if (Request.IsAjaxRequest())
return this.Json(new { Completed = true, Id = entity.Id });
return RedirectToAction("Details", new { id = entity.Id });
}
if (Request.IsAjaxRequest())
return PartialView("_Create", o1);
return View("Create", o1);
}
}
存储库是一个完全独立的项目,域也是如此。我的存储库代码已设置好,以便我可以使用一个存储库根据泛型查询任何对象:
public IQueryable<T> GetAll<T>() where T : AbstractEntity
{
return this.DbContext.Set<T>();
}
注意:AbstractEntity是我的所有POCO对象都继承自的域抽象类。
使用Moq对控制器进行单元测试时,一切都很好:
[TestMethod]
public void Create_Post_DuplicateNameAddsError()
{
// Arrange
var repository = new Mock<Repository.IRepository>();
repository.Setup(a => a.Any<Domain.Common.Object1>(It.IsAny<System.Linq.Expressions.Expression<Func<Domain.Common.Object1, bool>>>()))
.Returns(true);
var controller = ControllerFactory<MyController>.GetController();
controller.Repository = repository.Object;
var model = new Domain.Common.Object1()
{
Id = Guid.NewGuid()
,
Name = "Name"
};
// Act
var result = controller.Create(model) as ViewResult;
// Assert
Assert.IsFalse(controller.ModelState.IsValid);
Assert.IsNotNull(result);
Assert.AreEqual("Create", result.ViewName, false);
Assert.AreEqual(model, result.Model);
}
注意:ControllerFactory是一种生成具有某些属性的控制器的方法,例如Request,Response,User,Request.Headers等......
如果失败的话,我必须多次使用IRepository.Any(谓词),或者使用多次调用的表达式的任何方法。我需要它说出一个是真的而另一个是假的。如果表达式字符串是匹配的,那么这将是一个非问题,但由于一切都在不同的项目中,表达式字符串如下:
a => (a.Name.ToLower() == value(foo.Web.Tests.Controllers.Object1ControllerTests+<>c__DisplayClass3).ob1.Name.ToLower())
a => (a.Name.ToLower() == value(foo.Controllers.MyController+<>c__DisplayClass1).ob1.Name.ToLower())
不同之处在于价值功能。我尝试使用正则表达式进行匹配,但是很难看,因为你必须逃避每一个。&lt;&gt;(),这反过来又很难维护。
我尝试使用Matt Meber's Expression Equality Comparer,但由于该值函数(我的信念)它们不相等。
建议?