使用IMongoQueryable进行单元测试 System.InvalidCastException:无法将类型为EnumerableQuery的对象转换为IOrderedMongoQueryable类型

如何解决使用IMongoQueryable进行单元测试 System.InvalidCastException:无法将类型为EnumerableQuery的对象转换为IOrderedMongoQueryable类型

我使用GetAll()方法跟踪Service,并编写了测试此方法的测试。

public partial class DocumentTypeService : IDocumentTypeService
    {
        private readonly IRepository<DocumentType> _documentTypeRepository;
        private readonly IMediator _mediator;

        public DocumentTypeService(IRepository<DocumentType> documentTypeRepository,IMediator mediator)
        {
            _documentTypeRepository = documentTypeRepository;
            _mediator = mediator;
        }

        public virtual async Task<IList<DocumentType>> GetAll()
        {
            var query = from t in _documentTypeRepository.Table
                        orderby t.displayOrder
                        select t;
            return await query.ToListAsync();
        }
       
    }

这是我的测试方法GetAllDocumentTypes():

[TestClass()]
    public class DocumentTypeServiceTests
    {
        private Mock<IRepository<DocumentType>> _documentTypeRepositoryMock;
        private DocumentTypeService _documentTypeService;
        private Mock<IMediator> _mediatorMock;
        private Mock<IMongoQueryable<DocumentType>> _mongoQueryableMock;
        private List<DocumentType> _expected;
        private IQueryable<DocumentType> _expectedQueryable; 

        [Testinitialize()]
        public void Init()
        {
            _mediatorMock = new Mock<IMediator>();
            _documentTypeRepositoryMock = new Mock<IRepository<DocumentType>>();
            _mongoQueryableMock = new Mock<IMongoQueryable<DocumentType>>();
            _expected =  new List<DocumentType>
            {
                new DocumentType() {Name = "name1",Description = "t1",displayOrder = 0},new DocumentType() {Name = "name2",Description = "t2",displayOrder = 1}
            };
            _expectedQueryable = _expected.AsQueryable();
            _mongoQueryableMock.Setup(x => x.ElementType).Returns(_expectedQueryable.ElementType);
            _mongoQueryableMock.Setup(x => x.Expression).Returns(_expectedQueryable.Expression);
            _mongoQueryableMock.Setup(x => x.Provider).Returns(_expectedQueryable.Provider);
            _mongoQueryableMock.Setup(x => x.GetEnumerator()).Returns(_expectedQueryable.GetEnumerator());
                                  
            _documentTypeRepositoryMock.Setup(x => x.Table).Returns(_mongoQueryableMock.Object);
            _documentTypeService = new DocumentTypeService(_documentTypeRepositoryMock.Object,_mediatorMock.Object);
        }

        
        [TestMethod()]
        public async Task GetAllDocumentTypes()
        {
            var actual = await _documentTypeService.GetAll();
            Assert.AreEqual(_expected.Count,actual.Count);
        }
    }

获取错误

Message: 
    Test method Grand.Services.Tests.Documents.DocumentTypeServiceTests.GetAllDocumentTypes threw exception: 
    system.invalidCastException: Unable to cast object of type 'System.Linq.EnumerableQuery`1[Grand.Domain.Documents.DocumentType]' to type 'MongoDB.Driver.Linq.IOrderedMongoQueryable`1[Grand.Domain.Documents.DocumentType]'.
  Stack Trace: 
    MongoQueryable.OrderBy[TSource,TKey](IMongoQueryable`1 source,Expression`1 keySelector)
    DocumentTypeService.GetAll() line 38
    DocumentTypeServiceTests.GetAllDocumentTypes() line 101
    ThreadOperations.ExecuteWithAbortSafety(Action action)

能否请您解释一下为什么类型不是IOrderedMongoQueryable以及如何解决此问题?谢谢

解决方法

新(2020-09-15)

我尽可能地重现了您的努力。有两个问题。

首先,您的const v = [ [2,'apple'],[3,[4,[5,'banana'],[6,[7,'orange'],[8,[9,[10,] const reduced = v.reduce((ac,[num,fruit]) => ({ ...ac,[fruit]: [...(ac[fruit] || []),Number(num)] }),{}) const mapped = Object.entries(reduced).map(([fruit,v]) => ({ value: fruit,end: Math.max(...v),start: Math.min(...v) })) console.log('mapped',mapped)方法包含一个GetAll(),这正是迫使orderby成为IMongoQueryable的原因。但是,IOrderedMongoQueryable不会返回_mongoQueryableMock。而且,如果您尝试将IOrderedMongoQueryable的{​​{1}}替换为_mongoQueryableMock,那也会失败。我没有找到让IMongoQueryable允许IOrderedMongoQueryable的方法。

第二,异步可能会给您带来麻烦。在没有异步的情况下,我可以将_expectedQueryable查询更改为如下所示,这样可以在应用orderby之前解析查询:

IOrderedQueryable

但是,我没有找到一种管理ToListAsync的方法。

总而言之,我回到了最初的建议。不要尝试模拟IMongoQueryable。更改服务以接受MongoClient。无论哪种方式,您都在模拟Mongo,但是接受IMongoClient则是在直接使用它,而不是将其隐藏在另一个抽象后面。

GetAll()

原始(2020-09-14)

我不能完全确定,但是我相信是因为 public List<DocumentType> GetAll() { var query = from t in _documentTypeRepository.Table.ToList() orderby t.DisplayOrder select t; return query.ToList(); } 没有返回 private readonly IMongoClient _mongoClient; public DocumentTypeService(IMongoClient mongoClient) {...} 。它返回一个_documentTypeRepository,这将迫使有序mongo可查询。

要使用我here描述的模拟方法来工作,IMongoQueryable要么需要返回IMongoQueryable then 转换为Table,要么IQueryable可以转换为_documentTypeRepository

简而言之,Table的返回无可厚非。

老实说,所有这些都指向该存储库可能与实现紧密耦合。将IOrderedMongoQueryable强制为IOrderedMongoQueryable是一种代码异味和反模式。您可以考虑通过以下两种方式之一重新考虑服务和/或存储库层:

  1. 创建一个IMongoQueryable方法,将MongoDb结果映射到IQueryable。或
  2. 请勿在服务中使用存储库模式。相反,请注入IMongoClient,作为_documentTypeRepository.GetAll()方法的一部分从Mongo返回数据,并在那里映射到DocumentType。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?