使用Moq设置并验证表达式

6

有没有一种方法可以使用Moq设置和验证使用表达式的方法调用?

第一次尝试是我想让它工作的方式,而第二次尝试是一个“补丁”,让Assert部分工作(但验证部分仍然失败)。

string goodUrl = "good-product-url";

[Setup]
public void SetUp()
{
  productsQuery.Setup(x => x.GetByFilter(m=>m.Url== goodUrl).Returns(new Product() { Title = "Good product", ... });
}

[Test]
public void MyTest()
{
  var controller = GetController();
  var result = ((ViewResult)controller.Detail(goodUrl)).Model as ProductViewModel;
  Assert.AreEqual("Good product", result.Title);
  productsQuery.Verify(x => x.GetByFilter(t => t.Url == goodUrl), Times.Once());
}

这个测试在Assert失败并抛出了空引用异常,因为从未调用GetByFilter方法。

如果我使用这个:

[Setup]
public void SetUp()
{
  productsQuery.Setup(x => x.GetByFilter(It.IsAny<Expression<Func<Product, bool>>>())).Returns(new Product() { Title = "Good product", ... });
}

测试通过了断言部分,但是这次验证失败并提示从未被调用。是否有一种方法可以设置具有特定表达式的方法调用,而不是使用通用的It.IsAny<>()
更新:
我还尝试了评论中Ufuk Hacıoğulları的建议,并创建了以下内容。
Expression<Func<Product, bool>> goodUrlExpression = x => x.UrlRewrite == "GoodUrl";

[Setup]
public void SetUp()
{
  productsQuery.Setup(x => x.GetByFilter(goodUrlExpression)).Returns(new Product() { Title = "Good product", ... });
}

[Test]
public void MyTest()
{
  ...
  productsQuery.Verify(x => x.GetByFilter(goodUrlExpression), Times.Once());
}

但我遇到了空引用异常,就像第一次尝试一样。

我的控制器代码如下:

public ActionResult Detail(string urlRewrite)
{
  //Here, during tests, I get the null reference exception
  var entity = productQueries.GetByFilter(x => x.UrlRewrite == urlRewrite);
  var model = new ProductDetailViewModel() { UrlRewrite = entity.UrlRewrite, Culture = entity.Culture, Title = entity.Title };
  return View(model);
}

你能将lambda表达式分配给一个变量,并在Setup和Verify调用中使用它吗? - Ufuk Hacıoğulları
没有运气。请用您的建议更新问题。 - Iridio
我可能误解了你的意图,但如果你只是想验证设置是否匹配,那么你不能只使用VerifyAll吗? - DoctorMick
我猜我可以,但是设置从来没有被调用过。 - Iridio
你从哪里获取NRE? - Ufuk Hacıoğulları
在我的代码中,当我调用 productsQuery.GetByFilter(...) 时,我将会更新这个问题。 - Iridio
1个回答

8
以下代码演示了如何在此类情况下进行测试。 总体思想是对“真实”数据执行传入查询。这样,您甚至不需要“Verify”,因为如果查询不正确,则不会找到数据。
根据需要进行修改:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Moq;
using NUnit.Framework;

namespace StackOverflowExample.Moq
{
    public class Product
    {
        public string UrlRewrite { get; set; }
        public string Title { get; set; }
    }

    public interface IProductQuery
    {
        Product GetByFilter(Expression<Func<Product, bool>> filter);
    }

    public class Controller
    {
        private readonly IProductQuery _queryProvider;
        public Controller(IProductQuery queryProvider)
        {
            _queryProvider = queryProvider;
        }

        public Product GetProductByUrl(string urlRewrite)
        {
            return _queryProvider.GetByFilter(x => x.UrlRewrite == urlRewrite);
        }
    }

    [TestFixture]
    public class ExpressionMatching
    {
        [Test]
        public void MatchTest()
        {
            //arrange
            const string GOODURL = "goodurl";
            var goodProduct = new Product {UrlRewrite = GOODURL};
            var products = new List<Product>
                {
                    goodProduct
                };

            var qp = new Mock<IProductQuery>();
            qp.Setup(q => q.GetByFilter(It.IsAny<Expression<Func<Product, bool>>>()))
              .Returns<Expression<Func<Product, bool>>>(q =>
                  {
                      var query = q.Compile();
                      return products.First(query);
                  });

            var testController = new Controller(qp.Object);

            //act
            var foundProduct = testController.GetProductByUrl(GOODURL);

            //assert
            Assert.AreSame(foundProduct, goodProduct);
        }
    }
} 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接