using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace NUnit
{
public interface Enviromental
{
DateTime Now { get;}
void PlayWarn();
}
public class SystemEnvironment : Enviromental
{
public DateTime Now
{
get
{
return DateTime.Now;
}
}
public void PlayWarn()
{
}
}
public class MockSystemEnvironment : Enviromental
{
private DateTime currentTime;
public MockSystemEnvironment(DateTime when)
{
currentTime = when;
}
public DateTime Now
{
get
{
return currentTime;
}
}
public void IncreamentMinutes(int minutes)
{
currentTime = currentTime.AddMinutes(minutes);
}
public void PlayWarn()
{
MessageBox.Show("daoleshijian");
}
}
public class Checker
{
Enviromental env;
public Checker(Enviromental env)
{
this.env = env;
}
public void Reminder()
{
DateTime now = env.Now;
if (now.Hour >= 17)
{
env.PlayWarn();
}
}
}
}
1
//Test this mock time object
2
3
private void Test(object sender, EventArgs e)
4
{
5
DateTime when = new DateTime(2002, 10, 1, 17, 55, 0);
6
7
MockSystemEnvironment env;
8
env = new MockSystemEnvironment(when);
9
10
env.IncreamentMinutes(600);
11
Checker checker = new Checker(env);
12
13
listBox1.Items.Add(env.Now);
14
checker.Reminder();
15
}
//Test this mock time object2

3
private void Test(object sender, EventArgs e)4
{5
DateTime when = new DateTime(2002, 10, 1, 17, 55, 0);6

7
MockSystemEnvironment env;8
env = new MockSystemEnvironment(when);9

10
env.IncreamentMinutes(600);11
Checker checker = new Checker(env);12

13
listBox1.Items.Add(env.Now);14
checker.Reminder();15
}


浙公网安备 33010602011771号