0

我有一个视图,它实现了一个接口。

我正在尝试对此进行单元测试,但它变得很无聊......

声明是:

public interface IView : IBaseView
{
    TextBox ClientId { get; set; }
    TextBox ClientName { get; set; }
    Button SaveClient { get; set; }
    Button NextClient { get; set; }
    Button PreviousClient { get; set; }
    Button DiscardChanges {get;set;}
    bool ReadOnly { get; set;  }
    ListBox MyLittleList { get; set; }
}

    [Test]
    public void FirstSteps()
    {
        var sessionFactory = Substitute.For<ISessionFactory>();
        var session = Substitute.For<ISession>();
        var statelessSession = Substitute.For<IStatelessSession>();
        sessionFactory.OpenSession().Returns(session);
        sessionFactory.OpenStatelessSession().Returns(statelessSession);

        var view = Substitute.For<IView>();

        view.ClientId = new System.Windows.Forms.TextBox();
        view.ClientName = new System.Windows.Forms.TextBox();
        view.DiscardChanges = new System.Windows.Forms.Button();
        view.MyLittleList = new System.Windows.Forms.ListBox();
        view.NextClient = new System.Windows.Forms.Button();
        view.PreviousClient = new System.Windows.Forms.Button();
        view.ReadOnly = false;
        view.SaveClient = new System.Windows.Forms.Button();
    }

有没有一种观点让我动态地做到这一点?

将 View 传递给一个方法,该方法将验证那里有什么并自动调用构造函数并设置它?

4

1 回答 1

1

我不完全确定您在寻找什么,但也许这可能会有所帮助?:

public static void SetData<T>(T obj)
{
  foreach (var property in typeof(T).GetProperties())
    if (property.CanWrite && property.GetIndexParameters().Length == 0)
    {
      object val = null;

      //// Optionally some custom logic if you like:
      //if (property.PropertyType == typeof(string))
      //    val = "Jan-Peter Vos";
      //else

        val = Activator.CreateInstance(property.PropertyType);

      property.SetValue(obj, val, null);
    }
}

[Test]
public void FirstSteps()
{
  // .. Your code ..

  SetData(view);
}
于 2012-12-06T22:01:47.013 回答