[英]How to debug an application that is time-dependent? (using a current DateTime variable instead of DateTime.Now)
I'm working on an application (a web application, asp.net and c#) which is datetime-dependent, so, based on the current date, it will launch forms for the logged user to fill in. 我正在开发一个与日期时间相关的应用程序(Web应用程序,asp.net和c#),因此,根据当前日期,它将启动已登录用户填写的表单。
I've been thinking about how we're going to simulate real usage of the application, for debugging and testing purposes. 我一直在考虑如何模拟应用程序的实际使用情况,以进行调试和测试。
So I'm talking about replacing all those: 所以我在谈论取代所有这些:
DateTime currentDate = DateTime.Now;
with something like: 有类似的东西:
DateTime currentDate = MyDateClass.GetCurrentDate();
And then I'll have a class: 然后我会上课:
public class MyDateClass
{
private DateTime _currentDate;
public DateTime GetCurrentDate()
{
// get the date, which may be different from DateTime.Now
return _currentDate;
}
public void SetCurrentDate(DateTime newCurrentDate)
{
// set the date to the value chosen by the user
_currentDate = newCurrentDate;
}
}
allowing me to set the current data, by invoking the SetCurrentDate method, for example, in the code-behind of a link button and a calendar input. 允许我通过调用SetCurrentDate方法来设置当前数据,例如,在链接按钮和日历输入的代码隐藏中。
Question is... how should I exactly store the DateTime variable, throughout all the application? 问题是......在整个应用程序中,我应该如何准确存储DateTime变量? I can't work with the session in this class, right?
我不能在这堂课上工作,对吗? Should I work with the Thread?
我应该使用线程吗?
Well, ideas are appreciated :) Thanks in advance for your suggestions! 好吧,赞赏的想法:)提前感谢您的建议!
Some updating on my question: 我的问题有些更新:
I ran into this post: What's a good way to overwrite DateTime.Now during testing? 我遇到了这篇文章: 在测试期间覆盖DateTime.Now的好方法是什么?
Just like you provided here with your answers (thank you!), great tips for good code organization, for both development and testing purposes, that I will definitely be considering in the time to follow. 就像你在这里提供了你的答案(谢谢!),良好的代码组织技巧,无论是开发还是测试目的,我一定会考虑到时间。
I still have the same question though: how will I "hold" the datetime value? 我仍然有同样的问题:我将如何“保持”日期时间值?
Right now, I went with creating a single cell table in the data base to maintain "my" datetime value. 现在,我开始在数据库中创建一个单元格表来维护“我的”日期时间值。
I have a static class with a GetCurrentDate and a SetCurrentDate function: 我有一个带GetCurrentDate和SetCurrentDate函数的静态类:
public static class DateManagement
{
public static DateTime GetCurrentDate()
{
// return DateTime.Now;
// OR:
// ...
// SqlCommand cmd = new SqlCommand("select Date from CurrentDate", conn);
// ...
}
public static void SetCurrentDate(DateTime newDate) // used only in debugging
{
// ...
// string insertStr = @"update CurrentDate set Date = '" + date + "'";
// SqlCommand cmd = new SqlCommand(insertStr, conn);
// ...
}
}
However, storing the date in the database, with a table created and used just for debugging, doesn't seem like an elegant solution... 但是,将日期存储在数据库中,并创建一个仅用于调试的表,这似乎不是一个优雅的解决方案......
Create an interface: 创建一个界面:
public interface ITimeProvider
{
DateTime Now { get; }
}
Then you can create two subclasses - one for production usage which just returns the current time using DateTime.Now
, and another where you can manually set the time for testing purposes. 然后,您可以创建两个子类 - 一个用于生产用途,
DateTime.Now
使用DateTime.Now
返回当前时间,另一个子类可以手动设置测试时间。 Use dependency injection to provide the appropriate implementation. 使用依赖注入来提供适当的实现。
您是否可以使用Rhino Mocks(或类似软件)来欺骗单元测试中的当前日期时间,而不是仅仅为了测试目的而编写代码?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.