简体   繁体   中英

How do you best handle tests with dates?

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).

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.

How would you go about unit testing for such a thing... here is my mock transaction test repository.

public class TestTransactionsRepository : ITransactionsRepository
{
    private IList<Transaction> db;

    public TestTransactionsRepository()
    {
        db = new List<Transaction>();

        int i = 0;

        for (; i < 10; i++)
        {
            db.Add(CreateTransaction(i, 3));
        }

        for (; i < 25; i++)
        {
            db.Add(CreateTransaction(i, i));
        }

        for (; i < 80; i++)
        {
            db.Add(CreateTransaction(i, 5));
        }
    }

    private Transaction CreateTransaction(int id, int accountID)
    {
        return new Transaction
        {
            ID = id,
            AccountID = accountID,
            Date = ??
        };
     }
}

Here is an example test scnenario.

[TestMethod]
public void TransactionsRepository_Get_With_Filter_Between_ThisDate_
And_ThatDate_Returns_xx_Transactions()
{
    IList<Transaction> transactions = TransactionsRepository.Get()
                                        .Between(thisDate, thatDate)
                                        .ToList();

    Assert.AreEqual(xx, transactions.Count);
}

And then this is my filter method

public static IQueryable<Transaction> Between(
this IQueryable<Transaction> qry, DateTime startDate, DateTime endDate)
{
    return from t in qry
           where t.Date >= startDate && t.Date <= endDate
           select t;
}

您需要一种.Add (或CreateTransaction )的变体形式,使您可以“注入”(假)日期,正是出于这种测试目的。

您可以使用“ 如何对机器的特定行为进行单元测试? ”中所述的相同技术,以特定日期进行注入/使用/创建/测试。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM