我有一个View,它实现了一个接口。
我正在尝试对此进行单元测试,但它很无聊......
声明是:
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传递给一个方法,该方法将验证那里有什么并自动调用构造函数并设置它?
答案 0 :(得分: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);
}