代码之家  ›  专栏  ›  技术社区  ›  Aleksandar Vucetic

测试项目和配置文件

  •  2
  • Aleksandar Vucetic  · 技术社区  · 17 年前

    1 回复  |  直到 17 年前
        1
  •  2
  •   tvanfosson    17 年前

     public interface IMyConfig
     {
          string MyProperty { get; }
          int MyIntProperty { get; }
     }
    
     public class MyConfig : IMyConfig
     {
          public string MyProperty
          {
             get { ...lazy load from the actual config... }
          }
    
          public int MyIntProperty
          {
             get { ... }
          }
      }
    
     public class MyLibClass
     {
          private IMyConfig config;
    
          public MyLibClass() : this(null) {}
    
          public MyLibClass( IMyConfig config )
          {
               this.config = config ?? new MyConfig();
          }
    
          public void MyMethod()
          {
              string property = this.config.MyProperty;
    
              ...
          }
     }
    

    测试

     public void TestMethod()
     {
          IMyConfig config = MockRepository.GenerateMock<IMyConfig>();
          config.Expect( c => c.MyProperty ).Return( "stringValue" );
    
          var cls = new MyLib( config );
    
          cls.MyMethod();
    
          config.VerifyAllExpectations();
     }
    
    推荐文章