xunit System.ArgumentOutOfRangeException: '索引超出范围。必须为非负数且小于集合的大小。 (参数'index')'

xunit System.ArgumentOutOfRangeException: 'Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')'

您好,如您所见,我有这个测试计划:

[Fact]
        public async void Test1()
        {
            //Arange
            var data = new domain.Entities.ClientAppSettings() { ApplyCommissionInPortfolio = true, CreateDateTime  =System.DateTime.Now, DataTracker=false,
                LightTheme=false, NoBalance=false, NoSleep=false
            , Order=new OrderSettings() { BuyQuantity=1, DivideOrderToMultiple=false, OrderConfirmation=false, SellQuantity=12, PriceFromHeadline=false,
                Tick=121, TickType="" }, Notch=new NotchSettings() { Down=false,Up=true },PageSize=12,PortfolioBasedOnLastPositivePeriod=false,ShowNotifications=false,
                UseClosingPriceInPortfolioTotalValue=false,UserStatusBarToUp    =false
           };
            var mediator = new Mock<IMediator>();
            var userservice = new Mock<ICurrentUserService>();


            var isession = new Mock<IAsyncDocumentSession>();
            var itracer = new Mock<ITracer>();
            var ravenRepo = new RavenRepository<ClientAppSettings>(isession.Object,itracer.Object,true);
           


            var repoacc = new Mock<IRepositoryAccessor>();
            var repo = new Mock<domain.Interfaces.IRepository<ClientAppSettings>>();
            repo.Setup(i=>It.IsAny<IRepository<ClientAppSettings>>()).Returns(ravenRepo);

            repoacc.Setup(i => i.GetRepository<ClientAppSettings>(null,DatabaseType.Raven,null,false,true)).Returns(repo.Object);

            AddClientAppSettingCommand command = new AddClientAppSettingCommand(data);
            AddClientAppSettingCommandHandler handler = new AddClientAppSettingCommandHandler(userservice.Object, repoacc.Object);

            //Act
            var x = await handler.Handle(command, new System.Threading.CancellationToken());

            //Asert
            //Do the assertion

            //something like:
            //mediator.Verify(x => x.Publish(It.IsAny<CustomersChanged>()));
        }

但是在这一行

    repo.Setup(i=>It.IsAny<IRepository<ClientAppSettings>>()).Returns(ravenRepo);

我收到这个错误:

        repo.Setup(i=>It.IsAny<IRepository<ClientAppSettings>>()).Returns(ravenRepo);

   repo.Setup(i=>It.IsAny<IRepository<ClientAppSettings>>()).Returns(ravenRepo);

上面的表达是错误的,你没有提供你在这里设置的方法。

正确的版本应该类似于:

repo.Setup(i => i.AddOrUpdateAsync(It.IsAny<ClientAppSettings>())).Returns(Task.FromResult(OperationResult.Succeed()));

或者,您也可以使用 ReturnsAsync 方法:

repo.Setup(i => i.AddOrUpdateAsync(It.IsAny<ClientAppSettings>())).ReturnsAsync(OperationResult.Succeed());