public void ImportCustomers_InsertAndUpdate()
        {
            var loadedCustomers = new List <SAPCustomer>
            {
                new SAPCustomer {
                    CustomerId = null
                },                                    //should insert b/c CustomerId is null
                new SAPCustomer {
                    CustomerId = 1
                }                                 //should update b/c CustomerId is not null
            };

            _inputRepository.LoadCustomers().Returns(loadedCustomers);

            var result = _customerImportService.ImportCustomers();

            _inputRepository.Received(1).LoadCustomers();
            _outputRepository.ReceivedWithAnyArgs(1).InsertCustomers(null);
            _outputRepository.ReceivedWithAnyArgs(1).UpdateCustomers(null);
            //how do we know we called Insert/UpdateCustomers w/ correct param values?
            //b/c we have another UT for CustomerImportServicePure.DetermineInsertOrUpdate()
            //it would take more mocking and knowledge of NUnit to check that the methods were called with the correct param values.

            Assert.AreEqual(loadedCustomers, result); //we should return all customers that were loaded (because that's what we want to log)
        }
Beispiel #2
0
        //returning void b/c sproc currently does not handle errors
        public IEnumerable <SAPCustomer> ImportCustomers()
        {
            var loadedSAPCustomers = _inputRepository.LoadCustomers();

            var customersToWrite = _pureService.DetermineInsertOrUpdate(loadedSAPCustomers);

            //Note: another benefit of using a pure layer is better unit testing. This UT passes even if MapToCustomer throws NotImplementedException. Why? b/c the mocked repository doesn't do anything with its parameters, so the query is never evaluated
            var wcCustomersToInsert = _pureService.MapToWCCustomers(customersToWrite.CustomersToInsert);
            var wcCustomersToUpdate = _pureService.MapToWCCustomers(customersToWrite.CustomersToUpdate);

            //todo: procedural style
            //  * the input to UpdateCustomers is not the return value of InsertCustomers (nor should it be, b/c that wouldn't make sense)
            //  * db layer could have an upsert function, but then we'd be putting logic into the data layer. maybe that's actually good, b/c it's logic about which field is the primary key, and if it has a null value
            _outputRepository.InsertCustomers(wcCustomersToInsert);
            _outputRepository.UpdateCustomers(wcCustomersToUpdate);

            return(loadedSAPCustomers);
            //NO TRY/CATCH (to mimic existing sproc)
        }