乐乐

乐乐的博客园
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

How to constructor mock time object.

Posted on 2007-05-23 09:11  带你去月球  阅读(234)  评论(1编辑  收藏  举报

 

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
 3private void Test(object sender, EventArgs e)
 4{
 5    DateTime when = new DateTime(200210117550);
 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}
无觅相关文章插件,快速提升流量