我目前正在进入UnitTesting。像equals
这样的基本断言并不是什么大问题,但我在自定义textBox表单上引发焦点事件时遇到了麻烦。
对于UnitTesting我正在使用Microsoft.VisualStudio.TestTools.UnitTesting
。
以下是缩短的自定义TextBox类(事件本身正在运行 - 我已经通过将其添加到WinForm
以旧方式对其进行了测试):
public class ExtendedTextBox : TextBox
{
// ...
this.GotFocus += new EventHandler(this.This_OnFocus);
// ...
private void This_OnFocus(object sender, EventArgs e)
{
if (this.IsPlaceholderDefined() && this.Text.Trim() == this.Placeholder.Trim())
{
this.Text = "";
this.ForeColor = this.CustomDefaultForeColor;
}
}
// ...
}
以下是测试占位符功能的UnitTest类:
[TestClass]
public class ExtendedTextBoxTest
{
[TestMethod]
public void PlaceholderTest()
{
string placeholder = "Placeholder";
Color placeholderForeColor = Color.AliceBlue;
Color customDefaultForeColor = Color.Beige;
ExtendedTextBox extendedTextBox1 = new ExtendedTextBox(placeholder, placeholderForeColor, customDefaultForeColor);
Assert.IsTrue(extendedTextBox1.IsPlaceholderDefined());
Assert.AreEqual(placeholder, extendedTextBox1.Placeholder);
extendedTextBox1.Parent = new StartView();
extendedTextBox1.Focus();
Assert.AreNotEqual(placeholder, extendedTextBox1.Placeholder);
}
}
我已经在www中读到了一些代理内容等等,但我不知道如何将其包含在我的UnitTest-Method中。当然,我已经尝试了这个,但它没有帮助我。我还读到需要WinForm,这就是为什么我已将extendedTextBox1.Parent = new StartView();
添加到TestMethod
。
我感谢任何帮助:)
祝你好运 AMartinNo1