为什么这个模拟与表达式来; Func键< T,BOOL>是不是匹配? [英] Why this mock with Expression<Func<T,bool> is not matching?

查看:150
本文介绍了为什么这个模拟与表达式来; Func键< T,BOOL>是不是匹配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是在模仿新的,我试图做到这一点模拟例如:

i am new on mock and i am trying to do this mock example :

Repository.cs

Repository.cs

public class Repository : IRepository
{
    public List<Person> GetForExpression(Expression<Func<Person,bool>> expression  )
    {
        ... //get person from orm using expression
    }
}

PersonService.cs

PersonService.cs

public class PersonService
{
    private IRepository _repository;
    public PersonService(IRepository repository)
    {
        _repository = repository;
    }

    public List<Person> GetAllPersonsWith18More()
    {            
        var result = _repository.GetForExpression(x => x.Age > 18);

        return result;

    }
}



测试:

Test:

[TestClass]
public class UnitTest1
{
    [TestMethod]
    public void TestMethod1()
    {
        var moqRepository = new Mock<IRepository>();
        var service = new PersonService(moqRepository.Object);
        Expression<Func<Person, bool>> criteria = y => y.Age  18;
        moqRepository.Setup(x => x.GetForExpression(It.Is<Expression<Func<Person, bool>>>(y => y == criteria))).Returns(new List<Person>());
        service.GetAllPersonsWith18More();
        moqRepository.VerifyAll();
    }
}

如果我使用此设置工作:
moqRepository.Setup(X => x.GetForExpression(It.IsAny >>()))返回(新名单());

if i use this setup works: moqRepository.Setup(x => x.GetForExpression(It.IsAny>>())).Returns(new List());

但我想使用更多具体标准,这只是一个例子,我做证明我需要什么。

but i want to use more specific criteria, this is only one example that i do to demonstrate what i need.

这例子不匹配,谁能帮助理解为什么解决这个问题呢?

This example is not matching, can anyone help to understand why and solve this problem?

推荐答案

如果它是需要测试代表我通常把它们应用到任何测试对象。
为了测试PersonService我宁愿使用下面的代码,其中包含两个测试的例子。

If it is needed to test delegates I usually apply them to any test object. In order to test PersonService I'd rather use the following code that contains examples of two tests.

[TestClass]
public class UnitTest2
{
    private Mock<IRepository> moqRepository;
    private PersonService service;

    [TestInitialize]
    public void TestInitialize()
    {
        moqRepository = new Mock<IRepository>();
        service = new PersonService(moqRepository.Object);
    }

    [TestMethod]
    public void TestMethod3()
    {
        // arrange
        var persons = new List<Person>
        {
            new Person { Age = 0 },
            new Person { Age = 1 },
            new Person { Age = 17 },
            new Person { Age = 18 },
            new Person { Age = 19 },
            new Person { Age = 100 }
        };
        moqRepository
            .Setup(x => x.GetForExpression(It.IsAny<Expression<Func<Person, bool>>>()))
            .Returns<Expression<Func<Person, bool>>>(expr => persons.Where(expr.Compile()).ToList());

        // act
        var result = service.GetAllPersonsWith18More();

        // assert
        moqRepository.VerifyAll();
        result.Should().BeEquivalentTo(persons.Where(x => x.Age > 18));
    }

    [TestMethod]
    public void TestMethod4()
    {
        // arrange
        Expression<Func<Person, bool>> criteria = x => x.Age > 18;
        moqRepository
            .Setup(x => x.GetForExpression(It.IsAny<Expression<Func<Person, bool>>>()))
            .Returns(new List<Person>())
            .Callback<Expression<Func<Person, bool>>>(expr =>
            {
                var persons = new List<Person>
                {
                    new Person { Age = 0 },
                    new Person { Age = 1 },
                    new Person { Age = 17 },
                    new Person { Age = 18 },
                    new Person { Age = 19 },
                    new Person { Age = 100 }
                };
                persons.Where(expr.Compile()).Should().BeEquivalentTo(persons.Where(criteria.Compile()));
            });

        // act
        service.GetAllPersonsWith18More();

        // assert
        moqRepository.VerifyAll();
    }
}

这篇关于为什么这个模拟与表达式来; Func键&LT; T,BOOL&GT;是不是匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆