C# 无法强制转换类型为';System.Linq.EnumerableQuery`to type';MongoDB.Driver.Linq.IMongoQueryable`
我有一个具有以下方法句柄的类SendNotificationsToSubscribersCommandHandler,还有一个测试方法GetAllSubscriptionsWithCorrectParamsProductId(),用于检查句柄方法返回的列表是否正确 我发现以下错误:C# 无法强制转换类型为';System.Linq.EnumerableQuery`to type';MongoDB.Driver.Linq.IMongoQueryable`,c#,unit-testing,asp.net-core,.net-core,moq,C#,Unit Testing,Asp.net Core,.net Core,Moq,我有一个具有以下方法句柄的类SendNotificationsToSubscribersCommandHandler,还有一个测试方法GetAllSubscriptionsWithCorrectParamsProductId(),用于检查句柄方法返回的列表是否正确 我发现以下错误: Message: Test method Grand.Services.Tests.Commands.Handlers.Catalog.SendNotificationsToSubscribersComma
Message:
Test method Grand.Services.Tests.Commands.Handlers.Catalog.SendNotificationsToSubscribersCommandHandlerTest.GetAllSubscriptionsWithCorrectParamsProductId threw exception:
System.InvalidCastException: Unable to cast object of type 'System.Linq.EnumerableQuery`1[Grand.Domain.Catalog.BackInStockSubscription]' to type 'MongoDB.Driver.Linq.IMongoQueryable`1[Grand.Domain.Catalog.BackInStockSubscription]'.
Stack Trace:
MongoQueryable.Where[TSource](IMongoQueryable`1 source, Expression`1 predicate)
SendNotificationsToSubscribersCommandHandler
public class SendNotificationsToSubscribersCommandHandler : IRequestHandler<SendNotificationsToSubscribersCommand, IList<BackInStockSubscription>>
{
private readonly ICustomerService _customerService;
private readonly IWorkflowMessageService _workflowMessageService;
private readonly IRepository<BackInStockSubscription> _backInStockSubscriptionRepository;
public SendNotificationsToSubscribersCommandHandler(
ICustomerService customerService,
IWorkflowMessageService workflowMessageService,
IRepository<BackInStockSubscription> backInStockSubscriptionRepository)
{
_customerService = customerService;
_workflowMessageService = workflowMessageService;
_backInStockSubscriptionRepository = backInStockSubscriptionRepository;
}
public async Task<IList<BackInStockSubscription>> Handle(SendNotificationsToSubscribersCommand request, CancellationToken cancellationToken)
{
if (request.Product == null)
throw new ArgumentNullException("product");
int result = 0;
var query = _backInStockSubscriptionRepository.Table;
//product
query = query.Where(biss => biss.ProductId == request.Product.Id);
//warehouse
if (!string.IsNullOrEmpty(request.Warehouse))
query = query.Where(biss => biss.WarehouseId == request.Warehouse);
//warehouse
if (!string.IsNullOrEmpty(request.AttributeXml))
query = query.Where(biss => biss.AttributeXml == request.AttributeXml);
query = query.OrderByDescending(biss => biss.CreatedOnUtc);
var subscriptions = await query.ToListAsync();
//var subscriptions = await GetAllSubscriptionsByProductId(request.Product.Id, request.AttributeXml, request.Warehouse);
foreach (var subscription in subscriptions)
{
var customer = await _customerService.GetCustomerById(subscription.CustomerId);
//ensure that customer is registered (simple and fast way)
if (customer != null && CommonHelper.IsValidEmail(customer.Email))
{
var customerLanguageId = customer.GetAttributeFromEntity<string>(SystemCustomerAttributeNames.LanguageId, subscription.StoreId);
await _workflowMessageService.SendBackInStockNotification(customer, request.Product, subscription, customerLanguageId);
result++;
}
}
return subscriptions;
}
}
更新
调试应用程序时,属性\u backInStockSubscriptionRepository.Table
具有Expression.Value={aggregate([])}
如果我调试模拟对象的测试方法属性表有Expression.Value和Value=System.Collections.Generic.List
以及我的两个对象
非常感谢您的帮助请查看以下内容:
试着这样做:
_mongoQueryableMock = new Mock<IMongoQueryable<BackInStockSubscription>>();
_mongoQueryableMock.As<IEnumerable<BackInStockSubscription>>();
...
\u mongoQueryableMock=new Mock();
_mongoQueryableMock.As();
...
看看这里:
试着这样做:
_mongoQueryableMock = new Mock<IMongoQueryable<BackInStockSubscription>>();
_mongoQueryableMock.As<IEnumerable<BackInStockSubscription>>();
...
\u mongoQueryableMock=new Mock();
_mongoQueryableMock.As();
...
哪一行导致异常?方法句柄,行query=query.Where(biss=>biss.ProductId==ProductId);哪一行导致了异常?方法句柄,行query=query.Where(biss=>biss.ProductId==ProductId)<代码>\u mongoQueryableMock.As()代码>相同的错误。我很确定这与此有关,但我没有解决方案。尝试创建一个假的或存根,而不是使用Moq。只是为了测试。我想我已经找到了原因,但如何解决我发现很难回答。我更新了我的问题。你能检查一下吗。谢谢您您可以尝试发布您的IRepository.Table
的实现。但我看到其他人在嘲笑IMongoQueryable时遇到了问题。我也面临着同样的问题。。你能解决它吗?\u mongoQueryableMock.As()代码>相同的错误。我很确定这与此有关,但我没有解决方案。尝试创建一个假的或存根,而不是使用Moq。只是为了测试。我想我已经找到了原因,但如何解决我发现很难回答。我更新了我的问题。你能检查一下吗。谢谢您您可以尝试发布您的IRepository.Table
的实现。但我看到其他人在嘲笑IMongoQueryable时遇到了问题。我也面临着同样的问题。。你能解决它吗?
_mongoQueryableMock = new Mock<IMongoQueryable<BackInStockSubscription>>();
_mongoQueryableMock.As<IEnumerable<BackInStockSubscription>>();
...