title: Unit Testing Methods with Complex Linq Queries
body: <p>I feel like I have hit a bit of a brick wall with this program. This is my first attempt at unit testing anything, and I'm the guinea pig for the team. My issue revolves around unit testing methods with Linq queries, which call multiple Entities. I think I have figured out how to unit test a query using a single entity, but have yet to get multiple. </p>
  
  <p>Example code: </p>
  
  <pre><code>    [TestMethod]
      public void BillsTest_GetOutStandingBills_DataIntegrity()
      {
          //Arrange
          var data = new MockData();
          var billCache = data.MockBillCache();
  
          //var mockSet = new Mock&lt;DbSet&lt;BillCache&gt;&gt;();
          //mockSet.As&lt;IQueryable&lt;BillCache&gt;&gt;().Setup(m =&gt; m.Provider).Returns(dataBillCache.Provider);
          //mockSet.As&lt;IQueryable&lt;BillCache&gt;&gt;().Setup(m =&gt; m.Expression).Returns(dataBillCache.Expression);
          //mockSet.As&lt;IQueryable&lt;BillCache&gt;&gt;().Setup(m =&gt; m.ElementType).Returns(dataBillCache.ElementType);
          //mockSet.As&lt;IQueryable&lt;BillCache&gt;&gt;().Setup(m =&gt; m.GetEnumerator()).Returns(dataBillCache.GetEnumerator());
  
          var mockSet = EntityFrameworkMoqHelper.CreateMockForDbSet&lt;BillCache&gt;()
                                          .SetupForQueryOn(billCache);
  
  
          //var mockContext = new Mock&lt;BillPayContext&gt;();
          //mockContext.Setup(m =&gt; m.BillCaches).Returns(mockSet.Object);
  
          var mockContext = EntityFrameworkMoqHelper.CreateMockForDbContext&lt;BillPayContext, BillCache&gt;(mockSet); 
  
          string SSN = "556-53-08";
          var DOB = DateTime.Parse("11/02/1961");
          int MN = 43760148;
          var mockBills = new Bills(mockContext.Object);
  
          //Act
          var result = mockBills.GetOutStandingBills(SSN, DOB, MN);
          var confirm = result.ToList();
  
          //Assert
          Assert.AreEqual(2, confirm.Count);
  
  
      }
  </code></pre>
  
  <p>Method to be tested:</p>
  
  <pre><code>     public IEnumerable&lt;BillCache&gt; GetOutStandingBills(string SSN, DateTime DOB, int MN)
      {
          // Returns bills for the authenicated user by SSN and DOB or MRN and DOB
          IEnumerable&lt;BillCache&gt; outStandingBillsResult = null;
          if (!string.IsNullOrEmpty(SSN) &amp;&amp; DOB &gt; new DateTime(1753, 1, 1))
          {
              outStandingBillsResult = CoreBillQuery()
                      .Where(w =&gt; w.SSN == SSN &amp;&amp; w.DateOfBirth == DOB ||
                              (w.AccountID == MRN &amp;&amp; w.BillingSystemID == 9 &amp;&amp; w.DateOfBirth == DOB));
          }
  
          return outStandingBillsResult;            
      }
  </code></pre>