如何像这样模拟Expression <Func <object,bool >>> Is <Expression <Func <T,bool >>>

时间:2019-12-04 16:05:10

标签: c# .net moq xunit

我正在尝试模拟以下内容:

     [Fact]
    public void GetTipoBaixa()
    {
        var tipoBaixa = new TipoBaixa() { CodTipoBaixa = 1, DscTipoBaixa = "Sem Financeiro", IdTipoBaixa = 1 };
        string filtro = "Sem";

        Expression<Func<TipoBaixa, bool>> expression = ((TipoBaixa t) => t.DscTipoBaixa.ToString().ToUpper().Contains(filtro.ToUpper()));

             //_tipoBaixaRepository.Setup(s => s.Get(It.IsAny<Expression<Func<TipoBaixa, bool>>>(), null, null)).Returns(new List<TipoBaixa>()
        //{  tipoBaixa}.AsEnumerable());

        _tipoBaixaRepository.Setup(s => s.Get(It.Is<Expression<Func<TipoBaixa, bool>>>(expression), null, null)).Returns(new List<TipoBaixa>()
        {  tipoBaixa}.AsEnumerable());


        var tipoBaixaResult = _tipoBaixaService.Get(filtro).Content;

        var result = tipoBaixaResult.FirstOrDefault()?.IdTipoBaixa;

        Assert.Equal(tipoBaixa.IdTipoBaixa, result);
    }

这是我的服务获取:

   public IEnumerable<TipoBaixa> Get(string filtro)
    {
        Expression<Func<TipoBaixa, bool>> expression = ((TipoBaixa t) => t.DscTipoBaixa.ToString().ToUpper().Contains(filtro.ToUpper()));
        var response = this._repository.Get(expression, null, null);
        return response;
    }

这是我的存储库:

  public IEnumerable<TEntidade> Get(Expression<Func<TEntidade, bool>> predicate = null, Func<IQueryable<TEntidade>, IOrderedQueryable<TEntidade>> orderBy = null, Expression<Func<TEntidade, object>>[] includes = null)
    {
        var query = this._dbSet.AsQueryable();

        if (predicate != null)
            query = query.Where(predicate);

        if (orderBy != null)
            query = orderBy(query);

        if (includes != null)
            query = includes.Aggregate(query, (current, includeProperty) => current.Include(includeProperty));

        return query.AsEnumerable();
    }

但是出现以下错误:

  

System.Linq.Expressions.Expression<System.Func<Dominio.Entidades.TipoBaixa, bool>> to System.Linq.Expressions.Expression<System.Func<System.Linq.Expressions.Expression<System.Func<Dominio.Entidades.TipoBaixa, bool>>, bool>>

使用It.IsAny有效,但这不是我所需要的,因为我需要的是当有人更改服务中的过滤器时,我希望测试失败。

当我只在Mock中传递“表达式”时,该模拟将不起作用,因为它似乎不是“相同的表达式”。

0 个答案:

没有答案
相关问题