moq mongodb InsertOneAsync方法

时间:2019-02-11 11:57:56

标签: mongodb unit-testing .net-core moq

我只想使用mongodbContext插入方法。这是我正在尝试执行的操作,但不起作用。我对.net核心的Mongodb数据库非常陌生。任何帮助将不胜感激

   public void InsertEventAsync_Test()
        {
            //Arrange
            var eventRepository = EventRepository();
            var pEvent = new PlanEvent
            {
                ID = "testEvent",
                WorkOrderID = "WorkOrderID",
                IsDeleted = false,
                IsActive = true,
                EquipmentID = "EquipmentID"
            };
            ////Act


            //mockEventContext.Setup(mr => mr.PlanEvent.InsertOne(It.IsAny<PlanEvent>(), It.IsAny<InsertOneOptions>()))
            mockEventContext.Setup(s => s.PlanEvent.InsertOneAsync(It.IsAny<PlanEvent>(), It.IsAny<InsertOneOptions>())).Returns("sdad");
            var result = eventRepository.InsertEventAsync(pEvent);
            ////Assert
            result.Should().NotBeNull();

        }

下面是我需要起订量的方法

 public EventRepository(IFMPContext eventContext)
    {
        _eventContext = eventContext;
    }

   public async Task<string> InsertEventAsync(Model.EventDataModel.PlanEvent eventobj)
    {
        eventobj._id = ObjectId.GenerateNewId();
        eventobj.CreatedDateTime = DateTime.UtcNow.ToString();
        try
        {
            _eventContext.PlanEvent.InsertOne(eventobj);
            return eventobj.ID;
        }
        catch (Exception ex)
        {
            string x = ex.Message;
        }
        return "";

    }

感谢进阶

2 个答案:

答案 0 :(得分:1)

假设

public class EventRepository {
    private readonly IFMPContext eventContext;

    public EventRepository(IFMPContext eventContext) {
        this.eventContext = eventContext;
    }

    public async Task<string> InsertEventAsync(Model.EventDataModel.PlanEvent eventobj) {
        eventobj._id = ObjectId.GenerateNewId();
        eventobj.CreatedDateTime = DateTime.UtcNow.ToString();
        try {
            await eventContext.PlanEvent.InsertOneAsync(eventobj);
            return eventobj.ID;
        } catch (Exception ex) {
            string x = ex.Message;
        }
        return "";
    }
}

您需要配置测试以支持被测方法的异步性质

public async Task InsertEventAsync_Test()
{
    //Arrange
    var expected = "testEvent";
    var pEvent = new PlanEvent {
        ID = expected,
        WorkOrderID = "WorkOrderID",
        IsDeleted = false,
        IsActive = true,
        EquipmentID = "EquipmentID"
    };

    var mockEventContext = new Mock<IFMPContext>();
    mockEventContext
        .Setup(_ => _.PlanEvent.InsertOneAsync(It.IsAny<PlanEvent>(), It.IsAny<InsertOneOptions>()))
        .ReturnsAsync(Task.FromResult((object)null));

    var eventRepository = new EventRepository(mockEventContext.Object);

    //Act
    var actual = await eventRepository.InsertEventAsync(pEvent);

    //Assert
    actual.Should().NotBeNull()
    actual.Should().Be(expected);

}

需要将测试方法定义更新为异步的,以便等待被测方法。模拟依赖还需要以一种允许异步流在调用时按预期继续的方式进行设置。

答案 1 :(得分:0)

@Nkosi非常感谢您的帮助。终于我找到了路。我缺少下面的额外最小定量参数It.IsAny<System.Threading.CancellationToken>()是有效的测试

    public void InsertEventAsync_Test()
        {
            //Arrange
            var eventRepository = EventRepository();
            var pEvent = new PlanEvent
            {
                ID = "testEvent",
                WorkOrderID = "WorkOrderID",
                IsDeleted = false,
                IsActive = true,
                EquipmentID = "EquipmentID"
            };
            ////Act

            mockEventContext.Setup(s => s.PlanEvent.InsertOne(It.IsAny<PlanEvent>(), It.IsAny<InsertOneOptions>(),It.IsAny<System.Threading.CancellationToken>()));
            var result = eventRepository.InsertEventAsync(pEvent);
            ////Assert
            result.Should().NotBeNull();
            Assert.AreEqual(pEvent.ID, result);

        }