Moq - Linq expression in repository - Specify expression in setup

后端 未结 2 2048
梦谈多话
梦谈多话 2021-02-14 13:16

I have a method on my interface that looks like:

T GetSingle(Expression> criteria);

I\'m trying to mock the setup som

相关标签:
2条回答
  • 2021-02-14 13:56

    I managed to get this to work:

    Expression<Func<User, bool>> expr = user => user.EmailAddress == "a@b.com";
    
    _mockUserRepository.Setup(c => c.GetSingle(It.Is<Expression<Func<User, bool>>>(criteria => criteria == expr)))
        .Returns(new User { EmailAddress = "a@b.com" });
    
    User result = _mockUserRepository.Object.GetSingle(expr);
    
    0 讨论(0)
  • 2021-02-14 14:06

    If you don't mind a generic set up, it can be simpler like this.

    _mockUserRepository.Setup(c => c.GetSingle(It.IsAny<Expression<Func<User, bool>>>()))
        .Returns(new User { EmailAddress = "a@b.com" });
    
    0 讨论(0)
提交回复
热议问题