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", ... });
}

测试通过了 Assert 部分,但这次是 Verify that fail,表示它永远不会被调用。

有没有办法使用特定表达式而不是使用泛型来设置方法调用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);
}
4

1 回答 1

8

以下代码演示了如何在这种情况下进行测试。一般的想法是您对“真实”数据执行传入的查询。这样,你甚至不需要“验证”,如果查询不正确,它就不会找到数据。

修改以满足您的需求:

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);
        }
    }
} 
于 2013-07-10T19:44:15.617 回答