Пример #1
0
        // Railway Oriented Solution
        private CustomerCreatedViewModel RegisterCustomerSimple2_ROP(
            CreateCustomerViewModel createCustomerViewModel)
        {
            var customerResult = ValidateRop(createCustomerViewModel);
            var result         = customerResult
                                 .OnSuccess(c => _customerRepository.CreateRop(c))
                                 .OnSuccess(c => _mailConfirmer.SendWelcomeRop(c))
                                 .OnBoth(cResultAtEnd => cResultAtEnd.IsSuccess
                    ? new CustomerCreatedViewModel(cResultAtEnd.Value.Id)
                    : CreateErrorResponseRop(cResultAtEnd.Error));

            return(result);
        }
        public CustomerRegistrationTests()
        {
            var mapper = AutomapperConfiguration.Init();

            _customerRepository = Substitute.For <ICustomerRepository>();
            _creditCardGateway  = Substitute.For <ICreditCardGateway>();
            _mailConfirmer      = Substitute.For <IMailConfirmer>();

            // Default objects ('stubs')
            var customer = Mocks.GetCustomer();

            _vm = Mocks.GetCreateCustomerViewModel();
            _creditCardGatewayResponse = Mocks.GetCreditCardGatewayResponse();

            // Default system under test ('sut')
            _sut = new CustomerRegistration(mapper, _customerRepository,
                                            _mailConfirmer, _creditCardGateway);

            // Default Behaviour
            _customerRepository.Create(Arg.Any <Customer>()).Returns(customer);
            _customerRepository.CreateRop(Arg.Any <Customer>()).Returns(Result.Ok(customer));
            _customerRepository.UpgradeToPremium(Arg.Any <Customer>()).Returns(Result.Ok(customer));
            _creditCardGateway.Charge(Arg.Any <string>()).Returns(_creditCardGatewayResponse);
            _creditCardGateway.ChargeRop(Arg.Any <string>()).Returns(Result.Ok(_creditCardGatewayResponse));
            _creditCardGateway.RollBackLastTransactionRop(Arg.Any <Customer>()).Returns(Result.Ok(customer));

            _mailConfirmer.SendWelcomeRop(Arg.Any <Customer>()).Returns(Result.Ok(customer));
        }