【问题标题】:Mocking MongoDb Methods such as Find,FindAsync Method in XunitMocking MongoDb 方法,例如 Xunit 中的 Find、FindAsync 方法
【发布时间】:2019-11-21 15:29:51
【问题描述】:

我正在尝试模拟 mongodb 的 findasync,在 Xunit 和 .net 核心中查找方法。

当我试图模拟 InsertOne 时,

mockcollection.setup(x=>x.InsertOneAsync(_newitem,null,It.IsAny<CancellationToken>()).Returns(task.CompletedTask);

but the find is throwing error "Extension Method FindAsync may not be used in setup/verify process.

mockcollection.setup(x=>x.FindAsync(It.IsAny<FilterDefinition<mytbl>>(),null,It.IsAny<CancellationToken>()).Returns(Task.FromResult(mockasyncursor.Object));

当我在网上冲浪时,它都说不能模拟扩展方法,上面的方法[FindAsync] 是一个扩展方法,而InsertOne 不是。

如何模拟findasync 方法?

注意:我尝试使用Mongo2go 模拟数据库并能够得出积极的结果,但想知道如何使用模拟?

方法:

public async Task<IEnumerable<XX>> abc()
{
_logger.LogInformation("XXX");

var result = _context
        .XX.FindAsync(_ => true, null, CancellationToken.None);

return ( await _context.XX.FindAsync(_ => true) ).ToList<XX>();
}

单元测试方法:

public async Task XXX()
{
    // Arrange
    var XX = this.XX();
                < IAsyncCursor < XX >> mockasynccursor = new Mock<IAsyncCursor<XX>>();
    mockXXCollection = new Mock<IMongoCollection<XX>>();

    mockasynccursor.Setup(_ => _.Current).Returns(ReadfromJson());
    mockasynccursor.
        SetupSequence
        (_ => _.MoveNext(It.IsAny<CancellationToken>())).Returns(true).Returns(false);


    //sample
    var newitem = new XX { };
    mockXXCollection.
        Setup(x => x.InsertOneAsync(newitem, null, default(CancellationToken)))
        .Returns(Task.CompletedTask);

    //Error Here 
    mockXXCollection.Setup(x => x.FindAsync(It.IsAny<FilterDefinition<XX>>(), null, CancellationToken.None))
        .Returns(Task.FromResult(mockasynccursor.Object));

    //Message: System.NotSupportedException : Unsupported expression: x => x.FindAsync<XX>(It.IsAny<FilterDefinition<XX>>(), null, CancellationToken.None)
    //Extension methods( here: IMongoCollectionExtensions.FindAsync) may not be used in setup / verification expressions.



    mockStateFormContext.Setup(x => x.StateForms).Returns(mockXXCollection.Object);

    // Act
    var result = await xyzRepository.abc();

    // Assert

}

【问题讨论】:

    标签: c# mongodb unit-testing moq xunit


    【解决方案1】:

    实例FindAsync 定义如下所示

    Task<IAsyncCursor<TProjection>> FindAsync<TProjection>(
        FilterDefinition<TDocument> filter,
        FindOptions<TDocument, TProjection> options = null,
        CancellationToken cancellationToken = null
    )
    

    所有的扩展方法最终都会回调这个成员。

    在配置模拟时,请确保已明确设置实例成员

    //...
    
    mockXXCollection
        .Setup(_ => _.FindAsync(
            It.IsAny<FilterDefinition<XX>>(), 
            It.IsAny<FindOptions<XX, XX>>(), 
            It.IsAny<CancellationToken>()
        ))
        .ReturnsAsync(mockasynccursor.Object);
    
    //...
    

    【讨论】:

      猜你喜欢
      • 2017-09-08
      • 1970-01-01
      • 2014-05-06
      • 1970-01-01
      • 2019-11-05
      • 2018-04-28
      • 2017-02-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多