Ad
  • Custom User Avatar

    Linq is indeed a very effective technology

  • Custom User Avatar

    Dependency Injection also allows fakes/mocks to be injected in unit tests.

    Consider a function that processes orders.

    function ProcessOrders(orders) {
      var transactionApi = new transactionApi();
      
      for each (var order in orders) {
        if (!order.IsProcessed) {
          var transaction = new transaction(order);
          transactionApi.ProcessTransaction(transaction);
    }}}
    

    We'd like to write a test that verifies that an order that has already been processed is not processed again. But any test written against this function will actually hit the transaction API and process the transaction because it is tightly coupled to the function.

    Now let's refactor using Dependency Injection.

    function ProcessOrders(orders, transactionApi) {
      for each (var order in orders) {
        if (!order.IsProcessed) {
          var transaction = new transaction(order);
          transactionApi.ProcessTransaction(transaction);
    }}}
    

    By having a higher application layer do the binding of the transaction API to the ProcessOrders function, we can now write a test using a fake transaction API. This fake transactionApi won't actually process a transaction. Instead, it will only record when ProcessTransaction is called. And our test can use this record to verify the desired rules.

  • Custom User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Custom User Avatar

    Wow. It's like I copied you exactly. This is the easiest to understand solution IMO. Linq FTW.

  • Custom User Avatar

    endingSymbol should have been startingSymbol but meh, whatever.