Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do you best handle tests with dates?
    primarykey
    data
    text
    <p>I am using a Repository pattern with filters in my MVC application. The project displays a list of transactions where I can specify a date period to get a subset of transactions, or specify a specific date to get transactions for that date (including year, year/month, and year/month/day views).</p> <p>I also have functionality for paging getting the next and previous transactions based on what kind of view we are looking at. For example if we are selecting all transactions for a given year/month then I find the next previous and next transactions based on this date period.</p> <p>How would you go about unit testing for such a thing... here is my mock transaction test repository.</p> <pre> public class TestTransactionsRepository : ITransactionsRepository { private IList&lt;Transaction&gt; db; public TestTransactionsRepository() { db = new List&lt;Transaction&gt;(); int i = 0; for (; i &lt; 10; i++) { db.Add(CreateTransaction(i, 3)); } for (; i &lt; 25; i++) { db.Add(CreateTransaction(i, i)); } for (; i &lt; 80; i++) { db.Add(CreateTransaction(i, 5)); } } private Transaction CreateTransaction(int id, int accountID) { return new Transaction { ID = id, AccountID = accountID, Date = ?? }; } } </pre> <p>Here is an example test scnenario.</p> <pre> [TestMethod] public void TransactionsRepository_Get_With_Filter_Between_ThisDate_ And_ThatDate_Returns_xx_Transactions() { IList&lt;Transaction&gt; transactions = TransactionsRepository.Get() .Between(thisDate, thatDate) .ToList(); Assert.AreEqual(xx, transactions.Count); } </pre> <p>And then this is my filter method</p> <pre> public static IQueryable&lt;Transaction&gt; Between( this IQueryable&lt;Transaction&gt; qry, DateTime startDate, DateTime endDate) { return from t in qry where t.Date &gt;= startDate && t.Date &lt;= endDate select t; } </pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload