title: Unit Testing without Database: Linq to SQL
body: <p>I have  a repository implemented using LINQ to SQL. I need to do Unit Testing though I don't have a database. How can I write the UT for FreezeAllAccountsForUser method? Can you please show an example using manually mocking?</p>
  
  <p>Note: There is inheritance mapping used in domain objects </p>
  
  <p>Note: Unit Testing is to be done using Visual Studio Team Test </p>
  
  <blockquote>
    <p>Comment from @StuperUser. Unit testing involves completely isolating code from the other objects it interacts with. This means that if the code fails, you can be sure that the failure is to do with the code under test. To do this you have to fake these objects.</p>
  </blockquote>
  
  <p>CODE</p>
  
  <pre><code>     public void FreezeAllAccountsForUser(int userId)
      {
          List&lt;DTOLayer.BankAccountDTOForStatus&gt; bankAccountDTOList = new List&lt;DTOLayer.BankAccountDTOForStatus&gt;(); 
  
          IEnumerable&lt;DBML_Project.BankAccount&gt; accounts = AccountRepository.GetAllAccountsForUser(userId);
          foreach (DBML_Project.BankAccount acc in accounts)
          {
              string typeResult = Convert.ToString(acc.GetType());
              string baseValue = Convert.ToString(typeof(DBML_Project.BankAccount));
  
              if (String.Equals(typeResult, baseValue))
              {
                  throw new Exception("Not correct derived type");
              }
  
              acc.Freeze();
  
              DTOLayer.BankAccountDTOForStatus presentAccount = new DTOLayer.BankAccountDTOForStatus();
              presentAccount.BankAccountID = acc.BankAccountID;
              presentAccount.Status = acc.Status;
              bankAccountDTOList.Add(presentAccount);
  
          }
  
  
  
          IEnumerable&lt;System.Xml.Linq.XElement&gt; el = bankAccountDTOList.Select(x =&gt;
                          new System.Xml.Linq.XElement("BankAccountDTOForStatus",
                            new System.Xml.Linq.XElement("BankAccountID", x.BankAccountID),
                            new System.Xml.Linq.XElement("Status", x.Status)
                          ));
  
          System.Xml.Linq.XElement root = new System.Xml.Linq.XElement("root", el);
  
  
          //AccountRepository.UpdateBankAccountUsingParseXML_SP(root);
          AccountRepository.Update();
  
      }
  </code></pre>
  
  <p>Repository Layer</p>
  
  <pre><code>namespace RepositoryLayer
  {
  public interface ILijosBankRepository
  {
      System.Data.Linq.DataContext Context { get; set; }
      List&lt;DBML_Project.BankAccount&gt; GetAllAccountsForUser(int userID);
      void Update();
  
  }
  
  public class LijosSimpleBankRepository : ILijosBankRepository
  {
      public System.Data.Linq.DataContext Context
      {
          get;
          set;
      }
  
  
      public List&lt;DBML_Project.BankAccount&gt; GetAllAccountsForUser(int userID)
      {
          IQueryable&lt;DBML_Project.BankAccount&gt; queryResultEntities = Context.GetTable&lt;DBML_Project.BankAccount&gt;().Where(p =&gt; p.AccountOwnerID == userID);
          return queryResultEntities.ToList();
      }
  
  
      public virtual void Update()
      {
          //Context.SubmitChanges();
      }
  
  }
  
  }
  </code></pre>
  
  <p>Domain Classes</p>
  
  <pre><code>namespace DBML_Project
  {
  
  public  partial class BankAccount
  {
      //Define the domain behaviors
      public virtual void Freeze()
      {
          //Do nothing
      }
  }
  
  public class FixedBankAccount : BankAccount
  {
  
      public override void Freeze()
      {
          this.Status = "FrozenFA";
      }
  }
  
  public class SavingsBankAccount : BankAccount
  {
  
      public override void Freeze()
      {
          this.Status = "FrozenSB";
      }
  }  
  }
  </code></pre>
  
  <p>Auto generated Class by LINQ to SQL</p>
  
  <pre><code>[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.BankAccount")]
  [InheritanceMapping(Code = "Fixed", Type = typeof(FixedBankAccount), IsDefault = true)]
  [InheritanceMapping(Code = "Savings", Type = typeof(SavingsBankAccount))]
  public partial class BankAccount : INotifyPropertyChanging, INotifyPropertyChanged
  </code></pre>