简体   繁体   English

使用 lambda 表达式作为输入参数的 xUnit 模拟方法

[英]xUnit mock method with lambda expression as input parameter

I have a method like this one:我有一个这样的方法:

public Task<IEnumerable<VwSubscriptionProductsPf>> GetVwSubscriptionProductsBy(
    Expression<Func<VwSubscriptionProductsPf, object>> lambda)
{
    var result = GetList(lambda);
    return result;
}

The method is called like this:该方法是这样调用的:

var products = await _uow.VwSubscriptionProductsRepository
                   .GetVwSubscriptionProductsBy(s => s.SubscriptionId == subscription.Id);

And now I want to mock the return of the method using xUnit.现在我想使用 xUnit 来模拟方法的返回。

var products = new List<VwSubscriptionProductsPf> 
    { new VwSubscriptionProductsPf { SubscriptionId = 1, Name = "Product 1" }};

_uow.Setup(x => x.VwSubscriptionProductsRepository
    .GetVwSubscriptionProductsBy(s => s.SubscriptionId == 1))
    .Returns(Task.FromResult(products.AsEnumerable()));

Can you tell me what am I missing?你能告诉我我错过了什么吗?

PS.附注。 The problem is that the GetVwSubscriptionProductsBy returns an empty IEnumerable , and I'm waiting for the IEnumerable with one element manually created by me.问题是GetVwSubscriptionProductsBy返回一个空的IEnumerable ,我正在等待IEnumerable与我手动创建的一个元素。

I found the answer, the method can be mocked like this:我找到了答案,该方法可以这样模拟:

_uow.Setup(x => x.VwSubscriptionProductsRepository.GetVwSubscriptionProductsBy(It.IsAny<Expression<Func<VwSubscriptionProductsPf, object>>>()))
    .Returns(Task.FromResult(products.AsEnumerable()));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM