示例#1
0
        public async Task HandleShouldReturnOk()
        {
            // Arrange
            var mapperMock = new Mock <IMapper>();

            mapperMock.Setup(x => x.Map <IEnumerable <CustomerModel> >(It.IsAny <object>())).Returns(Enumerable.Empty <CustomerModel>());
            var mapper = mapperMock.Object;

            var customerReadRepositoryMock = new Mock <ICustomerReadRepository>();

            customerReadRepositoryMock.Setup(x => x.ListAsync(It.IsAny <Expression <Func <Customer, bool> > >(), It.IsAny <int?>(), It.IsAny <int?>())).Returns(Task.FromResult(Enumerable.Empty <Customer>()));
            var customerReadRepository = customerReadRepositoryMock.Object;

            var customerOdataProviderMock = new Mock <ICustomerOdataProvider>();

            customerOdataProviderMock.Setup(x => x.GetFilterPredicate(It.IsAny <string>())).Returns((Expression <Func <Customer, bool> >)null);
            var customerOdataProvider = customerOdataProviderMock.Object;

            var query = new ListCustomerQuery(null, null, null);

            var handler = new ListCustomerQueryHandler(mapper, customerReadRepository, customerOdataProvider);

            // Act
            var result = await handler.Handle(query, CancellationToken.None);

            // Assert
            result.IsFailure.Should().BeFalse();
            result.Should().BeOfType(typeof(Result <CustomersModel>));
        }
示例#2
0
        public async Task HandleShouldReturnFailWhenDatabaseSpecificErrorOccurs()
        {
            // Arrange
            var mapperMock = new Mock <IMapper>();

            mapperMock.Setup(x => x.Map <IEnumerable <CustomerModel> >(It.IsAny <object>())).Returns(Enumerable.Empty <CustomerModel>());
            var mapper = mapperMock.Object;

            var customerReadRepositoryMock = new Mock <ICustomerReadRepository>();

            customerReadRepositoryMock.Setup(x => x.ListAsync(It.IsAny <Expression <Func <Customer, bool> > >(), It.IsAny <int?>(), It.IsAny <int?>())).Throws <SomeDatabaseSpecificException>();
            var customerReadRepository = customerReadRepositoryMock.Object;

            var customerOdataProviderMock = new Mock <ICustomerOdataProvider>();

            customerOdataProviderMock.Setup(x => x.GetFilterPredicate(It.IsAny <string>())).Returns((Expression <Func <Customer, bool> >)null);
            var customerOdataProvider = customerOdataProviderMock.Object;

            var query = new ListCustomerQuery(null, null, null);

            var handler = new ListCustomerQueryHandler(mapper, customerReadRepository, customerOdataProvider);

            // Act
            var result = await handler.Handle(query, CancellationToken.None);

            // Assert
            result.IsFailure.Should().BeTrue();
            result.Failures.Should().OnlyContain(x => x.Message == CustomFailures.ListCustomerFailure);
        }